home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_glib / INCLUDE / GLIB.H < prev    next >
C/C++ Source or Header  |  1999-09-17  |  91KB  |  2,796 lines

  1. /* GLIB - Library of useful routines for C programming
  2.  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
  3.  *
  4.  * This library is free software; you can redistribute it and/or
  5.  * modify it under the terms of the GNU Library General Public
  6.  * License as published by the Free Software Foundation; either
  7.  * version 2 of the License, or (at your option) any later version.
  8.  *
  9.  * This library is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the GNU
  12.  * Library General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU Library General Public
  15.  * License along with this library; if not, write to the
  16.  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17.  * Boston, MA 02111-1307, USA.
  18.  */
  19.  
  20. /*
  21.  * Modified by the GLib Team and others 1997-1999.  See the AUTHORS
  22.  * file for a list of people on the GLib Team.  See the ChangeLog
  23.  * files for a list of changes.  These files are distributed with
  24.  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
  25.  */
  26.  
  27. #ifndef __G_LIB_H__
  28. #define __G_LIB_H__
  29.  
  30. /* system specific config file glibconfig.h provides definitions for
  31.  * the extrema of many of the standard types. These are:
  32.  *
  33.  *  G_MINSHORT, G_MAXSHORT
  34.  *  G_MININT, G_MAXINT
  35.  *  G_MINLONG, G_MAXLONG
  36.  *  G_MINFLOAT, G_MAXFLOAT
  37.  *  G_MINDOUBLE, G_MAXDOUBLE
  38.  *
  39.  * It also provides the following typedefs:
  40.  *
  41.  *  gint8, guint8
  42.  *  gint16, guint16
  43.  *  gint32, guint32
  44.  *  gint64, guint64
  45.  *
  46.  * It defines the G_BYTE_ORDER symbol to one of G_*_ENDIAN (see later in
  47.  * this file). 
  48.  *
  49.  * And it provides a way to store and retrieve a `gint' in/from a `gpointer'.
  50.  * This is useful to pass an integer instead of a pointer to a callback.
  51.  *
  52.  *  GINT_TO_POINTER(i), GUINT_TO_POINTER(i)
  53.  *  GPOINTER_TO_INT(p), GPOINTER_TO_UINT(p)
  54.  *
  55.  * Finally, it provide the following wrappers to STDC functions:
  56.  *
  57.  *  g_ATEXIT
  58.  *    To register hooks which are executed on exit().
  59.  *    Usually a wrapper for STDC atexit.
  60.  *
  61.  *  void *g_memmove(void *dest, const void *src, guint count);
  62.  *    A wrapper for STDC memmove, or an implementation, if memmove doesn't
  63.  *    exist.  The prototype looks like the above, give or take a const,
  64.  *    or size_t.
  65.  */
  66. #include <glibconfig.h>
  67.  
  68. /* include varargs functions for assertment macros
  69.  */
  70. #include <stdarg.h>
  71.  
  72. /* optionally feature DMALLOC memory allocation debugger
  73.  */
  74. #ifdef USE_DMALLOC
  75. #include "dmalloc.h"
  76. #endif
  77.  
  78.  
  79. #ifdef NATIVE_WIN32
  80.  
  81. /* On native Win32, directory separator is the backslash, and search path
  82.  * separator is the semicolon.
  83.  */
  84. #define G_DIR_SEPARATOR '\\'
  85. #define G_DIR_SEPARATOR_S "\\"
  86. #define G_SEARCHPATH_SEPARATOR ';'
  87. #define G_SEARCHPATH_SEPARATOR_S ";"
  88.  
  89. #else  /* !NATIVE_WIN32 */
  90.  
  91. /* Unix */
  92.  
  93. #define G_DIR_SEPARATOR '/'
  94. #define G_DIR_SEPARATOR_S "/"
  95. #define G_SEARCHPATH_SEPARATOR ':'
  96. #define G_SEARCHPATH_SEPARATOR_S ":"
  97.  
  98. #endif /* !NATIVE_WIN32 */
  99.  
  100. #ifdef __cplusplus
  101. extern "C" {
  102. #endif /* __cplusplus */
  103.  
  104.  
  105. /* Provide definitions for some commonly used macros.
  106.  *  Some of them are only provided if they haven't already
  107.  *  been defined. It is assumed that if they are already
  108.  *  defined then the current definition is correct.
  109.  */
  110. #ifndef    NULL
  111. #define    NULL    ((void*) 0)
  112. #endif
  113.  
  114. #ifndef    FALSE
  115. #define    FALSE    (0)
  116. #endif
  117.  
  118. #ifndef    TRUE
  119. #define    TRUE    (!FALSE)
  120. #endif
  121.  
  122. #undef    MAX
  123. #define MAX(a, b)  (((a) > (b)) ? (a) : (b))
  124.  
  125. #undef    MIN
  126. #define MIN(a, b)  (((a) < (b)) ? (a) : (b))
  127.  
  128. #undef    ABS
  129. #define ABS(a)       (((a) < 0) ? -(a) : (a))
  130.  
  131. #undef    CLAMP
  132. #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
  133.  
  134.  
  135. /* Define G_VA_COPY() to do the right thing for copying va_list variables.
  136.  * glibconfig.h may have already defined G_VA_COPY as va_copy or __va_copy.
  137.  */
  138. #if !defined (G_VA_COPY)
  139. #  if defined (__GNUC__) && defined (__PPC__) && (defined (_CALL_SYSV) || defined (_WIN32))
  140. #  define G_VA_COPY(ap1, ap2)      (*(ap1) = *(ap2))
  141. #  elif defined (G_VA_COPY_AS_ARRAY)
  142. #  define G_VA_COPY(ap1, ap2)      g_memmove ((ap1), (ap2), sizeof (va_list))
  143. #  else /* va_list is a pointer */
  144. #  define G_VA_COPY(ap1, ap2)      ((ap1) = (ap2))
  145. #  endif /* va_list is a pointer */
  146. #endif /* !G_VA_COPY */
  147.  
  148.  
  149. /* Provide convenience macros for handling structure
  150.  * fields through their offsets.
  151.  */
  152. #define G_STRUCT_OFFSET(struct_type, member)    \
  153.     ((gulong) ((gchar*) &((struct_type*) 0)->member))
  154. #define G_STRUCT_MEMBER_P(struct_p, struct_offset)   \
  155.     ((gpointer) ((gchar*) (struct_p) + (gulong) (struct_offset)))
  156. #define G_STRUCT_MEMBER(member_type, struct_p, struct_offset)   \
  157.     (*(member_type*) G_STRUCT_MEMBER_P ((struct_p), (struct_offset)))
  158.  
  159.  
  160. /* inlining hassle. for compilers that don't allow the `inline' keyword,
  161.  * mostly because of strict ANSI C compliance or dumbness, we try to fall
  162.  * back to either `__inline__' or `__inline'.
  163.  * we define G_CAN_INLINE, if the compiler seems to be actually
  164.  * *capable* to do function inlining, in which case inline function bodys
  165.  * do make sense. we also define G_INLINE_FUNC to properly export the
  166.  * function prototypes if no inlining can be performed.
  167.  * we special case most of the stuff, so inline functions can have a normal
  168.  * implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
  169.  */
  170. #ifndef G_INLINE_FUNC
  171. #  define G_CAN_INLINE 1
  172. #endif
  173. #ifdef G_HAVE_INLINE
  174. #  if defined (__GNUC__) && defined (__STRICT_ANSI__)
  175. #    undef inline
  176. #    define inline __inline__
  177. #  endif
  178. #else /* !G_HAVE_INLINE */
  179. #  undef inline
  180. #  if defined (G_HAVE___INLINE__)
  181. #    define inline __inline__
  182. #  else /* !inline && !__inline__ */
  183. #    if defined (G_HAVE___INLINE)
  184. #      define inline __inline
  185. #    else /* !inline && !__inline__ && !__inline */
  186. #      define inline /* don't inline, then */
  187. #      ifndef G_INLINE_FUNC
  188. #     undef G_CAN_INLINE
  189. #      endif
  190. #    endif
  191. #  endif
  192. #endif
  193. #ifndef G_INLINE_FUNC
  194. #  ifdef __GNUC__
  195. #    ifdef __OPTIMIZE__
  196. #      define G_INLINE_FUNC extern inline
  197. #    else
  198. #      undef G_CAN_INLINE
  199. #      define G_INLINE_FUNC extern
  200. #    endif
  201. #  else /* !__GNUC__ */
  202. #    ifdef G_CAN_INLINE
  203. #      define G_INLINE_FUNC static inline
  204. #    else
  205. #      define G_INLINE_FUNC extern
  206. #    endif
  207. #  endif /* !__GNUC__ */
  208. #endif /* !G_INLINE_FUNC */
  209.  
  210.  
  211. /* Provide simple macro statement wrappers (adapted from Perl):
  212.  *  G_STMT_START { statements; } G_STMT_END;
  213.  *  can be used as a single statement, as in
  214.  *  if (x) G_STMT_START { ... } G_STMT_END; else ...
  215.  *
  216.  *  For gcc we will wrap the statements within `({' and `})' braces.
  217.  *  For SunOS they will be wrapped within `if (1)' and `else (void) 0',
  218.  *  and otherwise within `do' and `while (0)'.
  219.  */
  220. #if !(defined (G_STMT_START) && defined (G_STMT_END))
  221. #  if defined (__GNUC__) && !defined (__STRICT_ANSI__) && !defined (__cplusplus)
  222. #    define G_STMT_START    (void)(
  223. #    define G_STMT_END        )
  224. #  else
  225. #    if (defined (sun) || defined (__sun__))
  226. #      define G_STMT_START    if (1)
  227. #      define G_STMT_END    else (void)0
  228. #    else
  229. #      define G_STMT_START    do
  230. #      define G_STMT_END    while (0)
  231. #    endif
  232. #  endif
  233. #endif
  234.  
  235.  
  236. /* Provide macros to feature the GCC function attribute.
  237.  */
  238. #if    __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4)
  239. #define G_GNUC_PRINTF( format_idx, arg_idx )    \
  240.   __attribute__((format (printf, format_idx, arg_idx)))
  241. #define G_GNUC_SCANF( format_idx, arg_idx )    \
  242.   __attribute__((format (scanf, format_idx, arg_idx)))
  243. #define G_GNUC_FORMAT( arg_idx )        \
  244.   __attribute__((format_arg (arg_idx)))
  245. #define G_GNUC_NORETURN                \
  246.   __attribute__((noreturn))
  247. #define G_GNUC_CONST                \
  248.   __attribute__((const))
  249. #define G_GNUC_UNUSED                \
  250.   __attribute__((unused))
  251. #else    /* !__GNUC__ */
  252. #define G_GNUC_PRINTF( format_idx, arg_idx )
  253. #define G_GNUC_SCANF( format_idx, arg_idx )
  254. #define G_GNUC_FORMAT( arg_idx )
  255. #define G_GNUC_NORETURN
  256. #define G_GNUC_CONST
  257. #define    G_GNUC_UNUSED
  258. #endif    /* !__GNUC__ */
  259.  
  260.  
  261. /* Wrap the gcc __PRETTY_FUNCTION__ and __FUNCTION__ variables with
  262.  * macros, so we can refer to them as strings unconditionally.
  263.  */
  264. #ifdef    __GNUC__
  265. #define    G_GNUC_FUNCTION        __FUNCTION__
  266. #define    G_GNUC_PRETTY_FUNCTION    __PRETTY_FUNCTION__
  267. #else    /* !__GNUC__ */
  268. #define    G_GNUC_FUNCTION        ""
  269. #define    G_GNUC_PRETTY_FUNCTION    ""
  270. #endif    /* !__GNUC__ */
  271.  
  272. /* we try to provide a usefull equivalent for ATEXIT if it is
  273.  * not defined, but use is actually abandoned. people should
  274.  * use g_atexit() instead.
  275.  */
  276. #ifndef ATEXIT
  277. # define ATEXIT(proc)    g_ATEXIT(proc)
  278. #else
  279. # define G_NATIVE_ATEXIT
  280. #endif /* ATEXIT */
  281.  
  282. /* Hacker macro to place breakpoints for elected machines.
  283.  * Actual use is strongly deprecated of course ;)
  284.  */
  285. #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
  286. #define    G_BREAKPOINT()        G_STMT_START{ __asm__ __volatile__ ("int $03"); }G_STMT_END
  287. #elif defined (__alpha__) && defined (__GNUC__) && __GNUC__ >= 2
  288. #define    G_BREAKPOINT()        G_STMT_START{ __asm__ __volatile__ ("bpt"); }G_STMT_END
  289. #else    /* !__i386__ && !__alpha__ */
  290. #define    G_BREAKPOINT()
  291. #endif    /* __i386__ */
  292.  
  293.  
  294. /* Provide macros for easily allocating memory. The macros
  295.  *  will cast the allocated memory to the specified type
  296.  *  in order to avoid compiler warnings. (Makes the code neater).
  297.  */
  298.  
  299. #ifdef __DMALLOC_H__
  300. #  define g_new(type, count)        (ALLOC (type, count))
  301. #  define g_new0(type, count)        (CALLOC (type, count))
  302. #  define g_renew(type, mem, count)    (REALLOC (mem, type, count))
  303. #else /* __DMALLOC_H__ */
  304. #  define g_new(type, count)      \
  305.       ((type *) g_malloc ((unsigned) sizeof (type) * (count)))
  306. #  define g_new0(type, count)      \
  307.       ((type *) g_malloc0 ((unsigned) sizeof (type) * (count)))
  308. #  define g_renew(type, mem, count)      \
  309.       ((type *) g_realloc (mem, (unsigned) sizeof (type) * (count)))
  310. #endif /* __DMALLOC_H__ */
  311.  
  312. #define g_mem_chunk_create(type, pre_alloc, alloc_type)    ( \
  313.   g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
  314.            sizeof (type), \
  315.            sizeof (type) * (pre_alloc), \
  316.            (alloc_type)) \
  317. )
  318. #define g_chunk_new(type, chunk)    ( \
  319.   (type *) g_mem_chunk_alloc (chunk) \
  320. )
  321. #define g_chunk_new0(type, chunk)    ( \
  322.   (type *) g_mem_chunk_alloc0 (chunk) \
  323. )
  324. #define g_chunk_free(mem, mem_chunk)    G_STMT_START { \
  325.   g_mem_chunk_free ((mem_chunk), (mem)); \
  326. } G_STMT_END
  327.  
  328.  
  329. #define g_string(x) #x
  330.  
  331.  
  332. /* Provide macros for error handling. The "assert" macros will
  333.  *  exit on failure. The "return" macros will exit the current
  334.  *  function. Two different definitions are given for the macros
  335.  *  if G_DISABLE_ASSERT is not defined, in order to support gcc's
  336.  *  __PRETTY_FUNCTION__ capability.
  337.  */
  338.  
  339. #ifdef G_DISABLE_ASSERT
  340.  
  341. #define g_assert(expr)
  342. #define g_assert_not_reached()
  343.  
  344. #else /* !G_DISABLE_ASSERT */
  345.  
  346. #ifdef __GNUC__
  347.  
  348. #define g_assert(expr)            G_STMT_START{        \
  349.      if (!(expr))                        \
  350.        g_log (G_LOG_DOMAIN,                    \
  351.           G_LOG_LEVEL_ERROR,                \
  352.           "file %s: line %d (%s): assertion failed: (%s)",    \
  353.           __FILE__,                        \
  354.           __LINE__,                        \
  355.           __PRETTY_FUNCTION__,                \
  356.           #expr);            }G_STMT_END
  357.  
  358. #define g_assert_not_reached()        G_STMT_START{        \
  359.      g_log (G_LOG_DOMAIN,                    \
  360.         G_LOG_LEVEL_ERROR,                    \
  361.         "file %s: line %d (%s): should not be reached",    \
  362.         __FILE__,                        \
  363.         __LINE__,                        \
  364.         __PRETTY_FUNCTION__);    }G_STMT_END
  365.  
  366. #else /* !__GNUC__ */
  367.  
  368. #define g_assert(expr)            G_STMT_START{        \
  369.      if (!(expr))                        \
  370.        g_log (G_LOG_DOMAIN,                    \
  371.           G_LOG_LEVEL_ERROR,                \
  372.           "file %s: line %d: assertion failed: (%s)",    \
  373.           __FILE__,                        \
  374.           __LINE__,                        \
  375.           #expr);            }G_STMT_END
  376.  
  377. #define g_assert_not_reached()        G_STMT_START{    \
  378.      g_log (G_LOG_DOMAIN,                \
  379.         G_LOG_LEVEL_ERROR,                \
  380.         "file %s: line %d: should not be reached",    \
  381.         __FILE__,                    \
  382.         __LINE__);        }G_STMT_END
  383.  
  384. #endif /* __GNUC__ */
  385.  
  386. #endif /* !G_DISABLE_ASSERT */
  387.  
  388.  
  389. #ifdef G_DISABLE_CHECKS
  390.  
  391. #define g_return_if_fail(expr)
  392. #define g_return_val_if_fail(expr,val)
  393.  
  394. #else /* !G_DISABLE_CHECKS */
  395.  
  396. #ifdef __GNUC__
  397.  
  398. #define g_return_if_fail(expr)        G_STMT_START{            \
  399.      if (!(expr))                            \
  400.        {                                \
  401.      g_log (G_LOG_DOMAIN,                        \
  402.         G_LOG_LEVEL_CRITICAL,                    \
  403.         "file %s: line %d (%s): assertion `%s' failed.",    \
  404.         __FILE__,                        \
  405.         __LINE__,                        \
  406.         __PRETTY_FUNCTION__,                    \
  407.         #expr);                            \
  408.      return;                            \
  409.        };                }G_STMT_END
  410.  
  411. #define g_return_val_if_fail(expr,val)    G_STMT_START{            \
  412.      if (!(expr))                            \
  413.        {                                \
  414.      g_log (G_LOG_DOMAIN,                        \
  415.         G_LOG_LEVEL_CRITICAL,                    \
  416.         "file %s: line %d (%s): assertion `%s' failed.",    \
  417.         __FILE__,                        \
  418.         __LINE__,                        \
  419.         __PRETTY_FUNCTION__,                    \
  420.         #expr);                            \
  421.      return val;                            \
  422.        };                }G_STMT_END
  423.  
  424. #else /* !__GNUC__ */
  425.  
  426. #define g_return_if_fail(expr)        G_STMT_START{        \
  427.      if (!(expr))                        \
  428.        {                            \
  429.      g_log (G_LOG_DOMAIN,                    \
  430.         G_LOG_LEVEL_CRITICAL,                \
  431.         "file %s: line %d: assertion `%s' failed.",    \
  432.         __FILE__,                    \
  433.         __LINE__,                    \
  434.         #expr);                        \
  435.      return;                        \
  436.        };                }G_STMT_END
  437.  
  438. #define g_return_val_if_fail(expr, val)    G_STMT_START{        \
  439.      if (!(expr))                        \
  440.        {                            \
  441.      g_log (G_LOG_DOMAIN,                    \
  442.         G_LOG_LEVEL_CRITICAL,                \
  443.         "file %s: line %d: assertion `%s' failed.",    \
  444.         __FILE__,                    \
  445.         __LINE__,                    \
  446.         #expr);                        \
  447.      return val;                        \
  448.        };                }G_STMT_END
  449.  
  450. #endif /* !__GNUC__ */
  451.  
  452. #endif /* !G_DISABLE_CHECKS */
  453.  
  454.  
  455. /* Provide type definitions for commonly used types.
  456.  *  These are useful because a "gint8" can be adjusted
  457.  *  to be 1 byte (8 bits) on all platforms. Similarly and
  458.  *  more importantly, "gint32" can be adjusted to be
  459.  *  4 bytes (32 bits) on all platforms.
  460.  */
  461.  
  462. typedef char   gchar;
  463. typedef short  gshort;
  464. typedef long   glong;
  465. typedef int    gint;
  466. typedef gint   gboolean;
  467.  
  468. typedef unsigned char    guchar;
  469. typedef unsigned short    gushort;
  470. typedef unsigned long    gulong;
  471. typedef unsigned int    guint;
  472.  
  473. typedef float    gfloat;
  474. typedef double    gdouble;
  475.  
  476. /* HAVE_LONG_DOUBLE doesn't work correctly on all platforms.
  477.  * Since gldouble isn't used anywhere, just disable it for now */
  478.  
  479. #if 0
  480. #ifdef HAVE_LONG_DOUBLE
  481. typedef long double gldouble;
  482. #else /* HAVE_LONG_DOUBLE */
  483. typedef double gldouble;
  484. #endif /* HAVE_LONG_DOUBLE */
  485. #endif /* 0 */
  486.  
  487. typedef void* gpointer;
  488. typedef const void *gconstpointer;
  489.  
  490.  
  491. typedef gint32    gssize;
  492. typedef guint32 gsize;
  493. typedef guint32 GQuark;
  494. typedef gint32    GTime;
  495.  
  496.  
  497. /* Portable endian checks and conversions
  498.  *
  499.  * glibconfig.h defines G_BYTE_ORDER which expands to one of
  500.  * the below macros.
  501.  */
  502. #define G_LITTLE_ENDIAN 1234
  503. #define G_BIG_ENDIAN    4321
  504. #define G_PDP_ENDIAN    3412        /* unused, need specific PDP check */    
  505.  
  506.  
  507. /* Basic bit swapping functions
  508.  */
  509. #define GUINT16_SWAP_LE_BE_CONSTANT(val)    ((guint16) ( \
  510.     (((guint16) (val) & (guint16) 0x00ffU) << 8) | \
  511.     (((guint16) (val) & (guint16) 0xff00U) >> 8)))
  512. #define GUINT32_SWAP_LE_BE_CONSTANT(val)    ((guint32) ( \
  513.     (((guint32) (val) & (guint32) 0x000000ffU) << 24) | \
  514.     (((guint32) (val) & (guint32) 0x0000ff00U) <<  8) | \
  515.     (((guint32) (val) & (guint32) 0x00ff0000U) >>  8) | \
  516.     (((guint32) (val) & (guint32) 0xff000000U) >> 24)))
  517.  
  518. /* Intel specific stuff for speed
  519.  */
  520. #if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
  521. #  define GUINT16_SWAP_LE_BE_X86(val) \
  522.      (__extension__                    \
  523.       ({ register guint16 __v;                \
  524.      if (__builtin_constant_p (val))        \
  525.        __v = GUINT16_SWAP_LE_BE_CONSTANT (val);    \
  526.      else                        \
  527.        __asm__ __const__ ("rorw $8, %w0"        \
  528.                   : "=r" (__v)        \
  529.                   : "0" ((guint16) (val)));    \
  530.     __v; }))
  531. #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_X86 (val))
  532. #  if !defined(__i486__) && !defined(__i586__) \
  533.       && !defined(__pentium__) && !defined(__i686__) && !defined(__pentiumpro__)
  534. #     define GUINT32_SWAP_LE_BE_X86(val) \
  535.         (__extension__                        \
  536.          ({ register guint32 __v;                \
  537.         if (__builtin_constant_p (val))            \
  538.           __v = GUINT32_SWAP_LE_BE_CONSTANT (val);        \
  539.       else                            \
  540.         __asm__ __const__ ("rorw $8, %w0\n\t"        \
  541.                    "rorl $16, %0\n\t"        \
  542.                    "rorw $8, %w0"            \
  543.                    : "=r" (__v)            \
  544.                    : "0" ((guint32) (val)));    \
  545.     __v; }))
  546. #  else /* 486 and higher has bswap */
  547. #     define GUINT32_SWAP_LE_BE_X86(val) \
  548.         (__extension__                        \
  549.          ({ register guint32 __v;                \
  550.         if (__builtin_constant_p (val))            \
  551.           __v = GUINT32_SWAP_LE_BE_CONSTANT (val);        \
  552.       else                            \
  553.         __asm__ __const__ ("bswap %0"            \
  554.                    : "=r" (__v)            \
  555.                    : "0" ((guint32) (val)));    \
  556.     __v; }))
  557. #  endif /* processor specific 32-bit stuff */
  558. #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_X86 (val))
  559. #else /* !__i386__ */
  560. #  define GUINT16_SWAP_LE_BE(val) (GUINT16_SWAP_LE_BE_CONSTANT (val))
  561. #  define GUINT32_SWAP_LE_BE(val) (GUINT32_SWAP_LE_BE_CONSTANT (val))
  562. #endif /* __i386__ */
  563.  
  564. #ifdef G_HAVE_GINT64
  565. #  define GUINT64_SWAP_LE_BE_CONSTANT(val)    ((guint64) ( \
  566.       (((guint64) (val) &                        \
  567.     (guint64) G_GINT64_CONSTANT(0x00000000000000ffU)) << 56) |    \
  568.       (((guint64) (val) &                        \
  569.     (guint64) G_GINT64_CONSTANT(0x000000000000ff00U)) << 40) |    \
  570.       (((guint64) (val) &                        \
  571.     (guint64) G_GINT64_CONSTANT(0x0000000000ff0000U)) << 24) |    \
  572.       (((guint64) (val) &                        \
  573.     (guint64) G_GINT64_CONSTANT(0x00000000ff000000U)) <<  8) |    \
  574.       (((guint64) (val) &                        \
  575.     (guint64) G_GINT64_CONSTANT(0x000000ff00000000U)) >>  8) |    \
  576.       (((guint64) (val) &                        \
  577.     (guint64) G_GINT64_CONSTANT(0x0000ff0000000000U)) >> 24) |    \
  578.       (((guint64) (val) &                        \
  579.     (guint64) G_GINT64_CONSTANT(0x00ff000000000000U)) >> 40) |    \
  580.       (((guint64) (val) &                        \
  581.     (guint64) G_GINT64_CONSTANT(0xff00000000000000U)) >> 56)))
  582. #  if defined (__i386__) && defined (__GNUC__) && __GNUC__ >= 2
  583. #    define GUINT64_SWAP_LE_BE_X86(val) \
  584.     (__extension__                        \
  585.      ({ union { guint64 __ll;                \
  586.             guint32 __l[2]; } __r;            \
  587.         if (__builtin_constant_p (val))            \
  588.           __r.__ll = GUINT64_SWAP_LE_BE_CONSTANT (val);    \
  589.         else                        \
  590.           {                            \
  591.          union { guint64 __ll;                \
  592.             guint32 __l[2]; } __w;            \
  593.         __w.__ll = ((guint64) val);            \
  594.         __r.__l[0] = GUINT32_SWAP_LE_BE (__w.__l[1]);    \
  595.         __r.__l[1] = GUINT32_SWAP_LE_BE (__w.__l[0]);    \
  596.           }                            \
  597.       __r.__ll; }))
  598. #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_X86 (val))
  599. #  else /* !__i386__ */
  600. #    define GUINT64_SWAP_LE_BE(val) (GUINT64_SWAP_LE_BE_CONSTANT(val))
  601. #  endif
  602. #endif
  603.  
  604. #define GUINT16_SWAP_LE_PDP(val)    ((guint16) (val))
  605. #define GUINT16_SWAP_BE_PDP(val)    (GUINT16_SWAP_LE_BE (val))
  606. #define GUINT32_SWAP_LE_PDP(val)    ((guint32) ( \
  607.     (((guint32) (val) & (guint32) 0x0000ffffU) << 16) | \
  608.     (((guint32) (val) & (guint32) 0xffff0000U) >> 16)))
  609. #define GUINT32_SWAP_BE_PDP(val)    ((guint32) ( \
  610.     (((guint32) (val) & (guint32) 0x00ff00ffU) << 8) | \
  611.     (((guint32) (val) & (guint32) 0xff00ff00U) >> 8)))
  612.  
  613. /* The G*_TO_?E() macros are defined in glibconfig.h.
  614.  * The transformation is symmetric, so the FROM just maps to the TO.
  615.  */
  616. #define GINT16_FROM_LE(val)    (GINT16_TO_LE (val))
  617. #define GUINT16_FROM_LE(val)    (GUINT16_TO_LE (val))
  618. #define GINT16_FROM_BE(val)    (GINT16_TO_BE (val))
  619. #define GUINT16_FROM_BE(val)    (GUINT16_TO_BE (val))
  620. #define GINT32_FROM_LE(val)    (GINT32_TO_LE (val))
  621. #define GUINT32_FROM_LE(val)    (GUINT32_TO_LE (val))
  622. #define GINT32_FROM_BE(val)    (GINT32_TO_BE (val))
  623. #define GUINT32_FROM_BE(val)    (GUINT32_TO_BE (val))
  624.  
  625. #ifdef G_HAVE_GINT64
  626. #define GINT64_FROM_LE(val)    (GINT64_TO_LE (val))
  627. #define GUINT64_FROM_LE(val)    (GUINT64_TO_LE (val))
  628. #define GINT64_FROM_BE(val)    (GINT64_TO_BE (val))
  629. #define GUINT64_FROM_BE(val)    (GUINT64_TO_BE (val))
  630. #endif
  631.  
  632. #define GLONG_FROM_LE(val)    (GLONG_TO_LE (val))
  633. #define GULONG_FROM_LE(val)    (GULONG_TO_LE (val))
  634. #define GLONG_FROM_BE(val)    (GLONG_TO_BE (val))
  635. #define GULONG_FROM_BE(val)    (GULONG_TO_BE (val))
  636.  
  637. #define GINT_FROM_LE(val)    (GINT_TO_LE (val))
  638. #define GUINT_FROM_LE(val)    (GUINT_TO_LE (val))
  639. #define GINT_FROM_BE(val)    (GINT_TO_BE (val))
  640. #define GUINT_FROM_BE(val)    (GUINT_TO_BE (val))
  641.  
  642.  
  643. /* Portable versions of host-network order stuff
  644.  */
  645. #define g_ntohl(val) (GUINT32_FROM_BE (val))
  646. #define g_ntohs(val) (GUINT16_FROM_BE (val))
  647. #define g_htonl(val) (GUINT32_TO_BE (val))
  648. #define g_htons(val) (GUINT16_TO_BE (val))
  649.  
  650.  
  651. /* Glib version.
  652.  * we prefix variable declarations so they can
  653.  * properly get exported in windows dlls.
  654.  */
  655. #ifdef NATIVE_WIN32
  656. #  ifdef GLIB_COMPILATION
  657. #    define GUTILS_C_VAR __declspec(dllexport)
  658. #  else /* !GLIB_COMPILATION */
  659. #    define GUTILS_C_VAR extern __declspec(dllimport)
  660. #  endif /* !GLIB_COMPILATION */
  661. #else /* !NATIVE_WIN32 */
  662. #  define GUTILS_C_VAR extern
  663. #endif /* !NATIVE_WIN32 */
  664.  
  665. GUTILS_C_VAR const guint glib_major_version;
  666. GUTILS_C_VAR const guint glib_minor_version;
  667. GUTILS_C_VAR const guint glib_micro_version;
  668. GUTILS_C_VAR const guint glib_interface_age;
  669. GUTILS_C_VAR const guint glib_binary_age;
  670.  
  671. #define GLIB_CHECK_VERSION(major,minor,micro)    \
  672.     (GLIB_MAJOR_VERSION > (major) || \
  673.      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION > (minor)) || \
  674.      (GLIB_MAJOR_VERSION == (major) && GLIB_MINOR_VERSION == (minor) && \
  675.       GLIB_MICRO_VERSION >= (micro)))
  676.  
  677. /* Forward declarations of glib types.
  678.  */
  679. typedef struct _GAllocator    GAllocator;
  680. typedef struct _GArray        GArray;
  681. typedef struct _GByteArray    GByteArray;
  682. typedef struct _GCache        GCache;
  683. typedef struct _GCompletion    GCompletion;
  684. typedef    struct _GData        GData;
  685. typedef struct _GDebugKey    GDebugKey;
  686. typedef struct _GHashTable    GHashTable;
  687. typedef struct _GHook        GHook;
  688. typedef struct _GHookList    GHookList;
  689. typedef struct _GList        GList;
  690. typedef struct _GMemChunk    GMemChunk;
  691. typedef struct _GNode        GNode;
  692. typedef struct _GPtrArray    GPtrArray;
  693. typedef struct _GRelation    GRelation;
  694. typedef struct _GScanner    GScanner;
  695. typedef struct _GScannerConfig    GScannerConfig;
  696. typedef struct _GSList        GSList;
  697. typedef struct _GString        GString;
  698. typedef struct _GStringChunk    GStringChunk;
  699. typedef struct _GTimer        GTimer;
  700. typedef struct _GTree        GTree;
  701. typedef struct _GTuples        GTuples;
  702. typedef union  _GTokenValue    GTokenValue;
  703. typedef struct _GIOChannel    GIOChannel;
  704.  
  705. /* Tree traverse flags */
  706. typedef enum
  707. {
  708.   G_TRAVERSE_LEAFS    = 1 << 0,
  709.   G_TRAVERSE_NON_LEAFS    = 1 << 1,
  710.   G_TRAVERSE_ALL    = G_TRAVERSE_LEAFS | G_TRAVERSE_NON_LEAFS,
  711.   G_TRAVERSE_MASK    = 0x03
  712. } GTraverseFlags;
  713.  
  714. /* Tree traverse orders */
  715. typedef enum
  716. {
  717.   G_IN_ORDER,
  718.   G_PRE_ORDER,
  719.   G_POST_ORDER,
  720.   G_LEVEL_ORDER
  721. } GTraverseType;
  722.  
  723. /* Log level shift offset for user defined
  724.  * log levels (0-7 are used by GLib).
  725.  */
  726. #define    G_LOG_LEVEL_USER_SHIFT    (8)
  727.  
  728. /* Glib log levels and flags.
  729.  */
  730. typedef enum
  731. {
  732.   /* log flags */
  733.   G_LOG_FLAG_RECURSION        = 1 << 0,
  734.   G_LOG_FLAG_FATAL        = 1 << 1,
  735.   
  736.   /* GLib log levels */
  737.   G_LOG_LEVEL_ERROR        = 1 << 2,    /* always fatal */
  738.   G_LOG_LEVEL_CRITICAL        = 1 << 3,
  739.   G_LOG_LEVEL_WARNING        = 1 << 4,
  740.   G_LOG_LEVEL_MESSAGE        = 1 << 5,
  741.   G_LOG_LEVEL_INFO        = 1 << 6,
  742.   G_LOG_LEVEL_DEBUG        = 1 << 7,
  743.   
  744.   G_LOG_LEVEL_MASK        = ~(G_LOG_FLAG_RECURSION | G_LOG_FLAG_FATAL)
  745. } GLogLevelFlags;
  746.  
  747. /* GLib log levels that are considered fatal by default */
  748. #define    G_LOG_FATAL_MASK    (G_LOG_FLAG_RECURSION | G_LOG_LEVEL_ERROR)
  749.  
  750.  
  751. typedef gpointer    (*GCacheNewFunc)    (gpointer    key);
  752. typedef gpointer    (*GCacheDupFunc)    (gpointer    value);
  753. typedef void        (*GCacheDestroyFunc)    (gpointer    value);
  754. typedef gint        (*GCompareFunc)        (gconstpointer    a,
  755.                          gconstpointer    b);
  756. typedef gchar*        (*GCompletionFunc)    (gpointer);
  757. typedef void        (*GDestroyNotify)    (gpointer    data);
  758. typedef void        (*GDataForeachFunc)    (GQuark        key_id,
  759.                          gpointer    data,
  760.                          gpointer    user_data);
  761. typedef void        (*GFunc)        (gpointer    data,
  762.                          gpointer    user_data);
  763. typedef guint        (*GHashFunc)        (gconstpointer    key);
  764. typedef void        (*GFreeFunc)        (gpointer    data);
  765. typedef void        (*GHFunc)        (gpointer    key,
  766.                          gpointer    value,
  767.                          gpointer    user_data);
  768. typedef gboolean    (*GHRFunc)        (gpointer    key,
  769.                          gpointer    value,
  770.                          gpointer    user_data);
  771. typedef gint        (*GHookCompareFunc)    (GHook        *new_hook,
  772.                          GHook        *sibling);
  773. typedef gboolean    (*GHookFindFunc)    (GHook        *hook,
  774.                          gpointer     data);
  775. typedef void        (*GHookMarshaller)    (GHook        *hook,
  776.                          gpointer     data);
  777. typedef gboolean    (*GHookCheckMarshaller)    (GHook        *hook,
  778.                          gpointer     data);
  779. typedef void        (*GHookFunc)        (gpointer     data);
  780. typedef gboolean    (*GHookCheckFunc)    (gpointer     data);
  781. typedef void        (*GHookFreeFunc)    (GHookList      *hook_list,
  782.                          GHook          *hook);
  783. typedef void        (*GLogFunc)        (const gchar   *log_domain,
  784.                          GLogLevelFlags    log_level,
  785.                          const gchar   *message,
  786.                          gpointer    user_data);
  787. typedef gboolean    (*GNodeTraverseFunc)    (GNode           *node,
  788.                          gpointer    data);
  789. typedef void        (*GNodeForeachFunc)    (GNode           *node,
  790.                          gpointer    data);
  791. typedef gint        (*GSearchFunc)        (gpointer    key,
  792.                          gpointer    data);
  793. typedef void        (*GScannerMsgFunc)    (GScanner      *scanner,
  794.                          gchar           *message,
  795.                          gint        error);
  796. typedef gint        (*GTraverseFunc)    (gpointer    key,
  797.                          gpointer    value,
  798.                          gpointer    data);
  799. typedef    void        (*GVoidFunc)        (void);
  800.  
  801.  
  802. struct _GList
  803. {
  804.   gpointer data;
  805.   GList *next;
  806.   GList *prev;
  807. };
  808.  
  809. struct _GSList
  810. {
  811.   gpointer data;
  812.   GSList *next;
  813. };
  814.  
  815. struct _GString
  816. {
  817.   gchar *str;
  818.   gint len;
  819. };
  820.  
  821. struct _GArray
  822. {
  823.   gchar *data;
  824.   guint len;
  825. };
  826.  
  827. struct _GByteArray
  828. {
  829.   guint8 *data;
  830.   guint      len;
  831. };
  832.  
  833. struct _GPtrArray
  834. {
  835.   gpointer *pdata;
  836.   guint        len;
  837. };
  838.  
  839. struct _GTuples
  840. {
  841.   guint len;
  842. };
  843.  
  844. struct _GDebugKey
  845. {
  846.   gchar *key;
  847.   guint     value;
  848. };
  849.  
  850.  
  851. /* Doubly linked lists
  852.  */
  853. void   g_list_push_allocator    (GAllocator     *allocator);
  854. void   g_list_pop_allocator     (void);
  855. GList* g_list_alloc        (void);
  856. void   g_list_free        (GList        *list);
  857. void   g_list_free_1        (GList        *list);
  858. GList* g_list_append        (GList        *list,
  859.                  gpointer     data);
  860. GList* g_list_prepend        (GList        *list,
  861.                  gpointer     data);
  862. GList* g_list_insert        (GList        *list,
  863.                  gpointer     data,
  864.                  gint         position);
  865. GList* g_list_insert_sorted    (GList        *list,
  866.                  gpointer     data,
  867.                  GCompareFunc     func);
  868. GList* g_list_concat        (GList        *list1,
  869.                  GList        *list2);
  870. GList* g_list_remove        (GList        *list,
  871.                  gpointer     data);
  872. GList* g_list_remove_link    (GList        *list,
  873.                  GList        *llink);
  874. GList* g_list_reverse        (GList        *list);
  875. GList* g_list_copy        (GList        *list);
  876. GList* g_list_nth        (GList        *list,
  877.                  guint         n);
  878. GList* g_list_find        (GList        *list,
  879.                  gpointer     data);
  880. GList* g_list_find_custom    (GList        *list,
  881.                  gpointer     data,
  882.                  GCompareFunc     func);
  883. gint   g_list_position        (GList        *list,
  884.                  GList        *llink);
  885. gint   g_list_index        (GList        *list,
  886.                  gpointer     data);
  887. GList* g_list_last        (GList        *list);
  888. GList* g_list_first        (GList        *list);
  889. guint  g_list_length        (GList        *list);
  890. void   g_list_foreach        (GList        *list,
  891.                  GFunc         func,
  892.                  gpointer     user_data);
  893. GList* g_list_sort              (GList          *list,
  894.                          GCompareFunc    compare_func);
  895. gpointer g_list_nth_data    (GList        *list,
  896.                  guint         n);
  897. #define g_list_previous(list)    ((list) ? (((GList *)(list))->prev) : NULL)
  898. #define g_list_next(list)    ((list) ? (((GList *)(list))->next) : NULL)
  899.  
  900.  
  901. /* Singly linked lists
  902.  */
  903. void    g_slist_push_allocator  (GAllocator     *allocator);
  904. void    g_slist_pop_allocator   (void);
  905. GSList* g_slist_alloc        (void);
  906. void    g_slist_free        (GSList        *list);
  907. void    g_slist_free_1        (GSList        *list);
  908. GSList* g_slist_append        (GSList        *list,
  909.                  gpointer     data);
  910. GSList* g_slist_prepend        (GSList        *list,
  911.                  gpointer     data);
  912. GSList* g_slist_insert        (GSList        *list,
  913.                  gpointer     data,
  914.                  gint         position);
  915. GSList* g_slist_insert_sorted    (GSList        *list,
  916.                  gpointer     data,
  917.                  GCompareFunc     func);
  918. GSList* g_slist_concat        (GSList        *list1,
  919.                  GSList        *list2);
  920. GSList* g_slist_remove        (GSList        *list,
  921.                  gpointer     data);
  922. GSList* g_slist_remove_link    (GSList        *list,
  923.                  GSList        *llink);
  924. GSList* g_slist_reverse        (GSList        *list);
  925. GSList*    g_slist_copy        (GSList        *list);
  926. GSList* g_slist_nth        (GSList        *list,
  927.                  guint         n);
  928. GSList* g_slist_find        (GSList        *list,
  929.                  gpointer     data);
  930. GSList* g_slist_find_custom    (GSList        *list,
  931.                  gpointer     data,
  932.                  GCompareFunc     func);
  933. gint    g_slist_position    (GSList        *list,
  934.                  GSList        *llink);
  935. gint    g_slist_index        (GSList        *list,
  936.                  gpointer     data);
  937. GSList* g_slist_last        (GSList        *list);
  938. guint    g_slist_length        (GSList        *list);
  939. void    g_slist_foreach        (GSList        *list,
  940.                  GFunc         func,
  941.                  gpointer     user_data);
  942. GSList*  g_slist_sort           (GSList          *list,
  943.                          GCompareFunc    compare_func);
  944. gpointer g_slist_nth_data    (GSList        *list,
  945.                  guint         n);
  946. #define g_slist_next(slist)    ((slist) ? (((GSList *)(slist))->next) : NULL)
  947.  
  948.  
  949. /* Hash tables
  950.  */
  951. GHashTable* g_hash_table_new        (GHashFunc     hash_func,
  952.                      GCompareFunc     key_compare_func);
  953. void        g_hash_table_destroy    (GHashTable    *hash_table);
  954. void        g_hash_table_insert        (GHashTable    *hash_table,
  955.                      gpointer     key,
  956.                      gpointer     value);
  957. void        g_hash_table_remove        (GHashTable    *hash_table,
  958.                      gconstpointer     key);
  959. gpointer    g_hash_table_lookup        (GHashTable    *hash_table,
  960.                      gconstpointer     key);
  961. gboolean    g_hash_table_lookup_extended(GHashTable    *hash_table,
  962.                      gconstpointer     lookup_key,
  963.                      gpointer    *orig_key,
  964.                      gpointer    *value);
  965. void        g_hash_table_freeze        (GHashTable    *hash_table);
  966. void        g_hash_table_thaw        (GHashTable    *hash_table);
  967. void        g_hash_table_foreach    (GHashTable    *hash_table,
  968.                      GHFunc         func,
  969.                      gpointer     user_data);
  970. guint        g_hash_table_foreach_remove    (GHashTable    *hash_table,
  971.                      GHRFunc     func,
  972.                      gpointer     user_data);
  973. guint        g_hash_table_size        (GHashTable    *hash_table);
  974.  
  975.  
  976. /* Caches
  977.  */
  978. GCache*     g_cache_new           (GCacheNewFunc       value_new_func,
  979.                 GCacheDestroyFunc  value_destroy_func,
  980.                 GCacheDupFunc       key_dup_func,
  981.                 GCacheDestroyFunc  key_destroy_func,
  982.                 GHashFunc       hash_key_func,
  983.                 GHashFunc       hash_value_func,
  984.                 GCompareFunc       key_compare_func);
  985. void     g_cache_destroy       (GCache          *cache);
  986. gpointer g_cache_insert           (GCache          *cache,
  987.                 gpointer       key);
  988. void     g_cache_remove           (GCache          *cache,
  989.                 gpointer       value);
  990. void     g_cache_key_foreach   (GCache          *cache,
  991.                 GHFunc           func,
  992.                 gpointer       user_data);
  993. void     g_cache_value_foreach (GCache          *cache,
  994.                 GHFunc           func,
  995.                 gpointer       user_data);
  996.  
  997.  
  998. /* Balanced binary trees
  999.  */
  1000. GTree*     g_tree_new     (GCompareFunc     key_compare_func);
  1001. void     g_tree_destroy     (GTree        *tree);
  1002. void     g_tree_insert     (GTree        *tree,
  1003.               gpointer     key,
  1004.               gpointer     value);
  1005. void     g_tree_remove     (GTree        *tree,
  1006.               gpointer     key);
  1007. gpointer g_tree_lookup     (GTree        *tree,
  1008.               gpointer     key);
  1009. void     g_tree_traverse (GTree        *tree,
  1010.               GTraverseFunc     traverse_func,
  1011.               GTraverseType     traverse_type,
  1012.               gpointer     data);
  1013. gpointer g_tree_search     (GTree        *tree,
  1014.               GSearchFunc     search_func,
  1015.               gpointer     data);
  1016. gint     g_tree_height     (GTree        *tree);
  1017. gint     g_tree_nnodes     (GTree        *tree);
  1018.  
  1019.  
  1020.  
  1021. /* N-way tree implementation
  1022.  */
  1023. struct _GNode
  1024. {
  1025.   gpointer data;
  1026.   GNode      *next;
  1027.   GNode      *prev;
  1028.   GNode      *parent;
  1029.   GNode      *children;
  1030. };
  1031.  
  1032. #define     G_NODE_IS_ROOT(node)    (((GNode*) (node))->parent == NULL && \
  1033.                  ((GNode*) (node))->prev == NULL && \
  1034.                  ((GNode*) (node))->next == NULL)
  1035. #define     G_NODE_IS_LEAF(node)    (((GNode*) (node))->children == NULL)
  1036.  
  1037. void     g_node_push_allocator  (GAllocator       *allocator);
  1038. void     g_node_pop_allocator   (void);
  1039. GNode*     g_node_new        (gpointer       data);
  1040. void     g_node_destroy        (GNode          *root);
  1041. void     g_node_unlink        (GNode          *node);
  1042. GNode*     g_node_insert        (GNode          *parent,
  1043.                  gint           position,
  1044.                  GNode          *node);
  1045. GNode*     g_node_insert_before    (GNode          *parent,
  1046.                  GNode          *sibling,
  1047.                  GNode          *node);
  1048. GNode*     g_node_prepend        (GNode          *parent,
  1049.                  GNode          *node);
  1050. guint     g_node_n_nodes        (GNode          *root,
  1051.                  GTraverseFlags       flags);
  1052. GNode*     g_node_get_root    (GNode          *node);
  1053. gboolean g_node_is_ancestor    (GNode          *node,
  1054.                  GNode          *descendant);
  1055. guint     g_node_depth        (GNode          *node);
  1056. GNode*     g_node_find        (GNode          *root,
  1057.                  GTraverseType       order,
  1058.                  GTraverseFlags       flags,
  1059.                  gpointer       data);
  1060.  
  1061. /* convenience macros */
  1062. #define g_node_append(parent, node)                \
  1063.      g_node_insert_before ((parent), NULL, (node))
  1064. #define    g_node_insert_data(parent, position, data)        \
  1065.      g_node_insert ((parent), (position), g_node_new (data))
  1066. #define    g_node_insert_data_before(parent, sibling, data)    \
  1067.      g_node_insert_before ((parent), (sibling), g_node_new (data))
  1068. #define    g_node_prepend_data(parent, data)            \
  1069.      g_node_prepend ((parent), g_node_new (data))
  1070. #define    g_node_append_data(parent, data)            \
  1071.      g_node_insert_before ((parent), NULL, g_node_new (data))
  1072.  
  1073. /* traversal function, assumes that `node' is root
  1074.  * (only traverses `node' and its subtree).
  1075.  * this function is just a high level interface to
  1076.  * low level traversal functions, optimized for speed.
  1077.  */
  1078. void     g_node_traverse    (GNode          *root,
  1079.                  GTraverseType       order,
  1080.                  GTraverseFlags       flags,
  1081.                  gint           max_depth,
  1082.                  GNodeTraverseFunc func,
  1083.                  gpointer       data);
  1084.  
  1085. /* return the maximum tree height starting with `node', this is an expensive
  1086.  * operation, since we need to visit all nodes. this could be shortened by
  1087.  * adding `guint height' to struct _GNode, but then again, this is not very
  1088.  * often needed, and would make g_node_insert() more time consuming.
  1089.  */
  1090. guint     g_node_max_height     (GNode *root);
  1091.  
  1092. void     g_node_children_foreach (GNode          *node,
  1093.                   GTraverseFlags   flags,
  1094.                   GNodeForeachFunc func,
  1095.                   gpointer       data);
  1096. void     g_node_reverse_children (GNode          *node);
  1097. guint     g_node_n_children     (GNode          *node);
  1098. GNode*     g_node_nth_child     (GNode          *node,
  1099.                   guint           n);
  1100. GNode*     g_node_last_child     (GNode          *node);
  1101. GNode*     g_node_find_child     (GNode          *node,
  1102.                   GTraverseFlags   flags,
  1103.                   gpointer       data);
  1104. gint     g_node_child_position     (GNode          *node,
  1105.                   GNode          *child);
  1106. gint     g_node_child_index     (GNode          *node,
  1107.                   gpointer       data);
  1108.  
  1109. GNode*     g_node_first_sibling     (GNode          *node);
  1110. GNode*     g_node_last_sibling     (GNode          *node);
  1111.  
  1112. #define     g_node_prev_sibling(node)    ((node) ? \
  1113.                      ((GNode*) (node))->prev : NULL)
  1114. #define     g_node_next_sibling(node)    ((node) ? \
  1115.                      ((GNode*) (node))->next : NULL)
  1116. #define     g_node_first_child(node)    ((node) ? \
  1117.                      ((GNode*) (node))->children : NULL)
  1118.  
  1119.  
  1120. /* Callback maintenance functions
  1121.  */
  1122. #define G_HOOK_FLAG_USER_SHIFT    (4)
  1123. typedef enum
  1124. {
  1125.   G_HOOK_FLAG_ACTIVE    = 1 << 0,
  1126.   G_HOOK_FLAG_IN_CALL    = 1 << 1,
  1127.   G_HOOK_FLAG_MASK    = 0x0f
  1128. } GHookFlagMask;
  1129.  
  1130. #define    G_HOOK_DEFERRED_DESTROY    ((GHookFreeFunc) 0x01)
  1131.  
  1132. struct _GHookList
  1133. {
  1134.   guint         seq_id;
  1135.   guint         hook_size;
  1136.   guint         is_setup : 1;
  1137.   GHook        *hooks;
  1138.   GMemChunk    *hook_memchunk;
  1139.   GHookFreeFunc     hook_free; /* virtual function */
  1140.   GHookFreeFunc     hook_destroy; /* virtual function */
  1141. };
  1142.  
  1143. struct _GHook
  1144. {
  1145.   gpointer     data;
  1146.   GHook        *next;
  1147.   GHook        *prev;
  1148.   guint         ref_count;
  1149.   guint         hook_id;
  1150.   guint         flags;
  1151.   gpointer     func;
  1152.   GDestroyNotify destroy;
  1153. };
  1154.  
  1155. #define    G_HOOK_ACTIVE(hook)        ((((GHook*) hook)->flags & \
  1156.                       G_HOOK_FLAG_ACTIVE) != 0)
  1157. #define    G_HOOK_IN_CALL(hook)        ((((GHook*) hook)->flags & \
  1158.                       G_HOOK_FLAG_IN_CALL) != 0)
  1159. #define G_HOOK_IS_VALID(hook)        (((GHook*) hook)->hook_id != 0 && \
  1160.                      G_HOOK_ACTIVE (hook))
  1161. #define G_HOOK_IS_UNLINKED(hook)    (((GHook*) hook)->next == NULL && \
  1162.                      ((GHook*) hook)->prev == NULL && \
  1163.                      ((GHook*) hook)->hook_id == 0 && \
  1164.                      ((GHook*) hook)->ref_count == 0)
  1165.  
  1166. void     g_hook_list_init        (GHookList        *hook_list,
  1167.                      guint             hook_size);
  1168. void     g_hook_list_clear        (GHookList        *hook_list);
  1169. GHook*     g_hook_alloc            (GHookList        *hook_list);
  1170. void     g_hook_free            (GHookList        *hook_list,
  1171.                      GHook            *hook);
  1172. void     g_hook_ref            (GHookList        *hook_list,
  1173.                      GHook            *hook);
  1174. void     g_hook_unref            (GHookList        *hook_list,
  1175.                      GHook            *hook);
  1176. gboolean g_hook_destroy            (GHookList        *hook_list,
  1177.                      guint             hook_id);
  1178. void     g_hook_destroy_link        (GHookList        *hook_list,
  1179.                      GHook            *hook);
  1180. void     g_hook_prepend            (GHookList        *hook_list,
  1181.                      GHook            *hook);
  1182. void     g_hook_insert_before        (GHookList        *hook_list,
  1183.                      GHook            *sibling,
  1184.                      GHook            *hook);
  1185. void     g_hook_insert_sorted        (GHookList        *hook_list,
  1186.                      GHook            *hook,
  1187.                      GHookCompareFunc     func);
  1188. GHook*     g_hook_get            (GHookList        *hook_list,
  1189.                      guint             hook_id);
  1190. GHook*     g_hook_find            (GHookList        *hook_list,
  1191.                      gboolean         need_valids,
  1192.                      GHookFindFunc         func,
  1193.                      gpointer         data);
  1194. GHook*     g_hook_find_data        (GHookList        *hook_list,
  1195.                      gboolean         need_valids,
  1196.                      gpointer         data);
  1197. GHook*     g_hook_find_func        (GHookList        *hook_list,
  1198.                      gboolean         need_valids,
  1199.                      gpointer         func);
  1200. GHook*     g_hook_find_func_data        (GHookList        *hook_list,
  1201.                      gboolean         need_valids,
  1202.                      gpointer         func,
  1203.                      gpointer         data);
  1204. /* return the first valid hook, and increment its reference count */
  1205. GHook*     g_hook_first_valid        (GHookList        *hook_list,
  1206.                      gboolean         may_be_in_call);
  1207. /* return the next valid hook with incremented reference count, and
  1208.  * decrement the reference count of the original hook
  1209.  */
  1210. GHook*     g_hook_next_valid        (GHookList        *hook_list,
  1211.                      GHook            *hook,
  1212.                      gboolean         may_be_in_call);
  1213.  
  1214. /* GHookCompareFunc implementation to insert hooks sorted by their id */
  1215. gint     g_hook_compare_ids        (GHook            *new_hook,
  1216.                      GHook            *sibling);
  1217.  
  1218. /* convenience macros */
  1219. #define     g_hook_append( hook_list, hook )  \
  1220.      g_hook_insert_before ((hook_list), NULL, (hook))
  1221.  
  1222. /* invoke all valid hooks with the (*GHookFunc) signature.
  1223.  */
  1224. void     g_hook_list_invoke        (GHookList        *hook_list,
  1225.                      gboolean         may_recurse);
  1226. /* invoke all valid hooks with the (*GHookCheckFunc) signature,
  1227.  * and destroy the hook if FALSE is returned.
  1228.  */
  1229. void     g_hook_list_invoke_check    (GHookList        *hook_list,
  1230.                      gboolean         may_recurse);
  1231. /* invoke a marshaller on all valid hooks.
  1232.  */
  1233. void     g_hook_list_marshal        (GHookList        *hook_list,
  1234.                      gboolean         may_recurse,
  1235.                      GHookMarshaller     marshaller,
  1236.                      gpointer         data);
  1237. void     g_hook_list_marshal_check    (GHookList        *hook_list,
  1238.                      gboolean         may_recurse,
  1239.                      GHookCheckMarshaller     marshaller,
  1240.                      gpointer         data);
  1241.  
  1242.  
  1243. /* Fatal error handlers.
  1244.  * g_on_error_query() will prompt the user to either
  1245.  * [E]xit, [H]alt, [P]roceed or show [S]tack trace.
  1246.  * g_on_error_stack_trace() invokes gdb, which attaches to the current
  1247.  * process and shows a stack trace.
  1248.  * These function may cause different actions on non-unix platforms.
  1249.  * The prg_name arg is required by gdb to find the executable, if it is
  1250.  * passed as NULL, g_on_error_query() will try g_get_prgname().
  1251.  */
  1252. void g_on_error_query (const gchar *prg_name);
  1253. void g_on_error_stack_trace (const gchar *prg_name);
  1254.  
  1255.  
  1256. /* Logging mechanism
  1257.  */
  1258. extern            const gchar        *g_log_domain_glib;
  1259. guint        g_log_set_handler    (const gchar    *log_domain,
  1260.                      GLogLevelFlags     log_levels,
  1261.                      GLogFunc     log_func,
  1262.                      gpointer     user_data);
  1263. void        g_log_remove_handler    (const gchar    *log_domain,
  1264.                      guint         handler_id);
  1265. void        g_log_default_handler    (const gchar    *log_domain,
  1266.                      GLogLevelFlags     log_level,
  1267.                      const gchar    *message,
  1268.                      gpointer     unused_data);
  1269. void        g_log            (const gchar    *log_domain,
  1270.                      GLogLevelFlags     log_level,
  1271.                      const gchar    *format,
  1272.                      ...) G_GNUC_PRINTF (3, 4);
  1273. void        g_logv            (const gchar    *log_domain,
  1274.                      GLogLevelFlags     log_level,
  1275.                      const gchar    *format,
  1276.                      va_list     args);
  1277. GLogLevelFlags    g_log_set_fatal_mask    (const gchar    *log_domain,
  1278.                      GLogLevelFlags     fatal_mask);
  1279. GLogLevelFlags    g_log_set_always_fatal    (GLogLevelFlags     fatal_mask);
  1280. #ifndef    G_LOG_DOMAIN
  1281. #define    G_LOG_DOMAIN    ((gchar*) 0)
  1282. #endif    /* G_LOG_DOMAIN */
  1283. #ifdef    __GNUC__
  1284. #define    g_error(format, args...)    g_log (G_LOG_DOMAIN, \
  1285.                            G_LOG_LEVEL_ERROR, \
  1286.                            format, ##args)
  1287. #define    g_message(format, args...)    g_log (G_LOG_DOMAIN, \
  1288.                            G_LOG_LEVEL_MESSAGE, \
  1289.                            format, ##args)
  1290. #define    g_warning(format, args...)    g_log (G_LOG_DOMAIN, \
  1291.                            G_LOG_LEVEL_WARNING, \
  1292.                            format, ##args)
  1293. #else    /* !__GNUC__ */
  1294. static void
  1295. g_error (const gchar *format,
  1296.      ...)
  1297. {
  1298.   va_list args;
  1299.   va_start (args, format);
  1300.   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_ERROR, format, args);
  1301.   va_end (args);
  1302. }
  1303. static void
  1304. g_message (const gchar *format,
  1305.        ...)
  1306. {
  1307.   va_list args;
  1308.   va_start (args, format);
  1309.   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, format, args);
  1310.   va_end (args);
  1311. }
  1312. static void
  1313. g_warning (const gchar *format,
  1314.        ...)
  1315. {
  1316.   va_list args;
  1317.   va_start (args, format);
  1318.   g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, format, args);
  1319.   va_end (args);
  1320. }
  1321. #endif    /* !__GNUC__ */
  1322.  
  1323. typedef void    (*GPrintFunc)        (const gchar    *string);
  1324. void        g_print            (const gchar    *format,
  1325.                      ...) G_GNUC_PRINTF (1, 2);
  1326. GPrintFunc    g_set_print_handler    (GPrintFunc     func);
  1327. void        g_printerr        (const gchar    *format,
  1328.                      ...) G_GNUC_PRINTF (1, 2);
  1329. GPrintFunc    g_set_printerr_handler    (GPrintFunc     func);
  1330.  
  1331. /* deprecated compatibility functions, use g_log_set_handler() instead */
  1332. typedef void        (*GErrorFunc)        (const gchar *str);
  1333. typedef void        (*GWarningFunc)        (const gchar *str);
  1334. GErrorFunc   g_set_error_handler   (GErrorFunc     func);
  1335. GWarningFunc g_set_warning_handler (GWarningFunc func);
  1336. GPrintFunc   g_set_message_handler (GPrintFunc func);
  1337.  
  1338.  
  1339. /* Memory allocation and debugging
  1340.  */
  1341. #ifdef USE_DMALLOC
  1342.  
  1343. #define g_malloc(size)         ((gpointer) MALLOC (size))
  1344. #define g_malloc0(size)         ((gpointer) CALLOC (char, size))
  1345. #define g_realloc(mem,size)  ((gpointer) REALLOC (mem, char, size))
  1346. #define g_free(mem)         FREE (mem)
  1347.  
  1348. #else /* !USE_DMALLOC */
  1349.  
  1350. gpointer g_malloc      (gulong      size);
  1351. gpointer g_malloc0     (gulong      size);
  1352. gpointer g_realloc     (gpointer  mem,
  1353.             gulong      size);
  1354. void     g_free           (gpointer  mem);
  1355.  
  1356. #endif /* !USE_DMALLOC */
  1357.  
  1358. void     g_mem_profile (void);
  1359. void     g_mem_check   (gpointer  mem);
  1360.  
  1361. /* Generic allocators
  1362.  */
  1363. GAllocator* g_allocator_new   (const gchar  *name,
  1364.                    guint         n_preallocs);
  1365. void        g_allocator_free  (GAllocator   *allocator);
  1366.  
  1367. #define    G_ALLOCATOR_LIST    (1)
  1368. #define    G_ALLOCATOR_SLIST    (2)
  1369. #define    G_ALLOCATOR_NODE    (3)
  1370.  
  1371.  
  1372. /* "g_mem_chunk_new" creates a new memory chunk.
  1373.  * Memory chunks are used to allocate pieces of memory which are
  1374.  *  always the same size. Lists are a good example of such a data type.
  1375.  * The memory chunk allocates and frees blocks of memory as needed.
  1376.  *  Just be sure to call "g_mem_chunk_free" and not "g_free" on data
  1377.  *  allocated in a mem chunk. ("g_free" will most likely cause a seg
  1378.  *  fault...somewhere).
  1379.  *
  1380.  * Oh yeah, GMemChunk is an opaque data type. (You don't really
  1381.  *  want to know what's going on inside do you?)
  1382.  */
  1383.  
  1384. /* ALLOC_ONLY MemChunk's can only allocate memory. The free operation
  1385.  *  is interpreted as a no op. ALLOC_ONLY MemChunk's save 4 bytes per
  1386.  *  atom. (They are also useful for lists which use MemChunk to allocate
  1387.  *  memory but are also part of the MemChunk implementation).
  1388.  * ALLOC_AND_FREE MemChunk's can allocate and free memory.
  1389.  */
  1390.  
  1391. #define G_ALLOC_ONLY      1
  1392. #define G_ALLOC_AND_FREE  2
  1393.  
  1394. GMemChunk* g_mem_chunk_new     (gchar      *name,
  1395.                 gint       atom_size,
  1396.                 gulong       area_size,
  1397.                 gint       type);
  1398. void       g_mem_chunk_destroy (GMemChunk *mem_chunk);
  1399. gpointer   g_mem_chunk_alloc   (GMemChunk *mem_chunk);
  1400. gpointer   g_mem_chunk_alloc0  (GMemChunk *mem_chunk);
  1401. void       g_mem_chunk_free    (GMemChunk *mem_chunk,
  1402.                 gpointer   mem);
  1403. void       g_mem_chunk_clean   (GMemChunk *mem_chunk);
  1404. void       g_mem_chunk_reset   (GMemChunk *mem_chunk);
  1405. void       g_mem_chunk_print   (GMemChunk *mem_chunk);
  1406. void       g_mem_chunk_info    (void);
  1407.  
  1408. /* Ah yes...we have a "g_blow_chunks" function.
  1409.  * "g_blow_chunks" simply compresses all the chunks. This operation
  1410.  *  consists of freeing every memory area that should be freed (but
  1411.  *  which we haven't gotten around to doing yet). And, no,
  1412.  *  "g_blow_chunks" doesn't follow the naming scheme, but it is a
  1413.  *  much better name than "g_mem_chunk_clean_all" or something
  1414.  *  similar.
  1415.  */
  1416. void g_blow_chunks (void);
  1417.  
  1418.  
  1419. /* Timer
  1420.  */
  1421. GTimer* g_timer_new    (void);
  1422. void    g_timer_destroy (GTimer     *timer);
  1423. void    g_timer_start    (GTimer     *timer);
  1424. void    g_timer_stop    (GTimer     *timer);
  1425. void    g_timer_reset    (GTimer     *timer);
  1426. gdouble g_timer_elapsed (GTimer     *timer,
  1427.              gulong     *microseconds);
  1428.  
  1429.  
  1430. /* String utility functions that modify a string argument or
  1431.  * return a constant string that must not be freed.
  1432.  */
  1433. #define     G_STR_DELIMITERS    "_-|> <."
  1434. gchar*     g_strdelimit        (gchar         *string,
  1435.                  const gchar *delimiters,
  1436.                  gchar          new_delimiter);
  1437. gdouble     g_strtod        (const gchar *nptr,
  1438.                  gchar        **endptr);
  1439. gchar*     g_strerror        (gint          errnum);
  1440. gchar*     g_strsignal        (gint          signum);
  1441. gint     g_strcasecmp        (const gchar *s1,
  1442.                  const gchar *s2);
  1443. gint     g_strncasecmp        (const gchar *s1,
  1444.                  const gchar *s2,
  1445.                  guint           n);
  1446. void     g_strdown        (gchar         *string);
  1447. void     g_strup        (gchar         *string);
  1448. void     g_strreverse        (gchar         *string);
  1449. /* removes leading spaces */
  1450. gchar*   g_strchug              (gchar        *string);
  1451. /* removes trailing spaces */
  1452. gchar*  g_strchomp              (gchar        *string);
  1453. /* removes leading & trailing spaces */
  1454. #define g_strstrip( string )    g_strchomp (g_strchug (string))
  1455.  
  1456. /* String utility functions that return a newly allocated string which
  1457.  * ought to be freed from the caller at some point.
  1458.  */
  1459. gchar*     g_strdup        (const gchar *str);
  1460. gchar*     g_strdup_printf    (const gchar *format,
  1461.                  ...) G_GNUC_PRINTF (1, 2);
  1462. gchar*     g_strdup_vprintf    (const gchar *format,
  1463.                  va_list      args);
  1464. gchar*     g_strndup        (const gchar *str,
  1465.                  guint          n);
  1466. gchar*     g_strnfill        (guint          length,
  1467.                  gchar          fill_char);
  1468. gchar*     g_strconcat        (const gchar *string1,
  1469.                  ...); /* NULL terminated */
  1470. gchar*   g_strjoin        (const gchar  *separator,
  1471.                  ...); /* NULL terminated */
  1472. gchar*     g_strescape        (gchar          *string);
  1473. gpointer g_memdup        (gconstpointer mem,
  1474.                  guint           byte_size);
  1475.  
  1476. /* NULL terminated string arrays.
  1477.  * g_strsplit() splits up string into max_tokens tokens at delim and
  1478.  * returns a newly allocated string array.
  1479.  * g_strjoinv() concatenates all of str_array's strings, sliding in an
  1480.  * optional separator, the returned string is newly allocated.
  1481.  * g_strfreev() frees the array itself and all of its strings.
  1482.  */
  1483. gchar**     g_strsplit        (const gchar  *string,
  1484.                  const gchar  *delimiter,
  1485.                  gint          max_tokens);
  1486. gchar*   g_strjoinv        (const gchar  *separator,
  1487.                  gchar       **str_array);
  1488. void     g_strfreev        (gchar       **str_array);
  1489.  
  1490.  
  1491.  
  1492. /* calculate a string size, guarranteed to fit format + args.
  1493.  */
  1494. guint    g_printf_string_upper_bound (const gchar* format,
  1495.                      va_list      args);
  1496.  
  1497.  
  1498. /* Retrive static string info
  1499.  */
  1500. gchar*    g_get_user_name        (void);
  1501. gchar*    g_get_real_name        (void);
  1502. gchar*    g_get_home_dir        (void);
  1503. gchar*    g_get_tmp_dir        (void);
  1504. gchar*    g_get_prgname        (void);
  1505. void    g_set_prgname        (const gchar *prgname);
  1506.  
  1507.  
  1508. /* Miscellaneous utility functions
  1509.  */
  1510. guint    g_parse_debug_string    (const gchar *string,
  1511.                  GDebugKey   *keys,
  1512.                  guint          nkeys);
  1513. gint    g_snprintf        (gchar         *string,
  1514.                  gulong          n,
  1515.                  gchar const *format,
  1516.                  ...) G_GNUC_PRINTF (3, 4);
  1517. gint    g_vsnprintf        (gchar         *string,
  1518.                  gulong          n,
  1519.                  gchar const *format,
  1520.                  va_list      args);
  1521. gchar*    g_basename        (const gchar *file_name);
  1522. /* Check if a file name is an absolute path */
  1523. gboolean g_path_is_absolute    (const gchar *file_name);
  1524. /* In case of absolute paths, skip the root part */
  1525. gchar*  g_path_skip_root    (gchar       *file_name);
  1526.  
  1527. /* strings are newly allocated with g_malloc() */
  1528. gchar*    g_dirname        (const gchar *file_name);
  1529. gchar*    g_get_current_dir    (void);
  1530. gchar*  g_getenv        (const gchar *variable);
  1531.  
  1532.  
  1533. /* we use a GLib function as a replacement for ATEXIT, so
  1534.  * the programmer is not required to check the return value
  1535.  * (if there is any in the implementation) and doesn't encounter
  1536.  * missing include files.
  1537.  */
  1538. void    g_atexit        (GVoidFunc    func);
  1539.  
  1540.  
  1541. /* Bit tests
  1542.  */
  1543. G_INLINE_FUNC gint    g_bit_nth_lsf (guint32 mask,
  1544.                        gint    nth_bit);
  1545. #ifdef    G_CAN_INLINE
  1546. G_INLINE_FUNC gint
  1547. g_bit_nth_lsf (guint32 mask,
  1548.            gint    nth_bit)
  1549. {
  1550.   do
  1551.     {
  1552.       nth_bit++;
  1553.       if (mask & (1 << (guint) nth_bit))
  1554.     return nth_bit;
  1555.     }
  1556.   while (nth_bit < 32);
  1557.   return -1;
  1558. }
  1559. #endif    /* G_CAN_INLINE */
  1560.  
  1561. G_INLINE_FUNC gint    g_bit_nth_msf (guint32 mask,
  1562.                        gint    nth_bit);
  1563. #ifdef G_CAN_INLINE
  1564. G_INLINE_FUNC gint
  1565. g_bit_nth_msf (guint32 mask,
  1566.            gint    nth_bit)
  1567. {
  1568.   if (nth_bit < 0)
  1569.     nth_bit = 32;
  1570.   do
  1571.     {
  1572.       nth_bit--;
  1573.       if (mask & (1 << (guint) nth_bit))
  1574.     return nth_bit;
  1575.     }
  1576.   while (nth_bit > 0);
  1577.   return -1;
  1578. }
  1579. #endif    /* G_CAN_INLINE */
  1580.  
  1581. G_INLINE_FUNC guint    g_bit_storage (guint number);
  1582. #ifdef G_CAN_INLINE
  1583. G_INLINE_FUNC guint
  1584. g_bit_storage (guint number)
  1585. {
  1586.   register guint n_bits = 0;
  1587.   
  1588.   do
  1589.     {
  1590.       n_bits++;
  1591.       number >>= 1;
  1592.     }
  1593.   while (number);
  1594.   return n_bits;
  1595. }
  1596. #endif    /* G_CAN_INLINE */
  1597.  
  1598. /* String Chunks
  1599.  */
  1600. GStringChunk* g_string_chunk_new       (gint size);
  1601. void          g_string_chunk_free       (GStringChunk *chunk);
  1602. gchar*          g_string_chunk_insert       (GStringChunk *chunk,
  1603.                         const gchar     *string);
  1604. gchar*          g_string_chunk_insert_const  (GStringChunk *chunk,
  1605.                         const gchar     *string);
  1606.  
  1607.  
  1608. /* Strings
  1609.  */
  1610. GString* g_string_new        (const gchar *init);
  1611. GString* g_string_sized_new (guint      dfl_size);
  1612. void     g_string_free        (GString     *string,
  1613.                  gint      free_segment);
  1614. GString* g_string_assign    (GString     *lval,
  1615.                  const gchar *rval);
  1616. GString* g_string_truncate  (GString     *string,
  1617.                  gint      len);
  1618. GString* g_string_append    (GString     *string,
  1619.                  const gchar *val);
  1620. GString* g_string_append_c  (GString     *string,
  1621.                  gchar      c);
  1622. GString* g_string_prepend   (GString     *string,
  1623.                  const gchar *val);
  1624. GString* g_string_prepend_c (GString     *string,
  1625.                  gchar      c);
  1626. GString* g_string_insert    (GString     *string,
  1627.                  gint      pos,
  1628.                  const gchar *val);
  1629. GString* g_string_insert_c  (GString     *string,
  1630.                  gint      pos,
  1631.                  gchar      c);
  1632. GString* g_string_erase        (GString     *string,
  1633.                  gint      pos,
  1634.                  gint      len);
  1635. GString* g_string_down        (GString     *string);
  1636. GString* g_string_up        (GString     *string);
  1637. void     g_string_sprintf   (GString     *string,
  1638.                  const gchar *format,
  1639.                  ...) G_GNUC_PRINTF (2, 3);
  1640. void     g_string_sprintfa  (GString     *string,
  1641.                  const gchar *format,
  1642.                  ...) G_GNUC_PRINTF (2, 3);
  1643.  
  1644.  
  1645. /* Resizable arrays, remove fills any cleared spot and shortens the
  1646.  * array, while preserving the order. remove_fast will distort the
  1647.  * order by moving the last element to the position of the removed 
  1648.  */
  1649.  
  1650. #define g_array_append_val(a,v)      g_array_append_vals (a, &v, 1)
  1651. #define g_array_prepend_val(a,v)  g_array_prepend_vals (a, &v, 1)
  1652. #define g_array_insert_val(a,i,v) g_array_insert_vals (a, i, &v, 1)
  1653. #define g_array_index(a,t,i)      (((t*) (a)->data) [(i)])
  1654.  
  1655. GArray* g_array_new              (gboolean        zero_terminated,
  1656.                    gboolean        clear,
  1657.                    guint        element_size);
  1658. void    g_array_free              (GArray       *array,
  1659.                    gboolean        free_segment);
  1660. GArray* g_array_append_vals       (GArray       *array,
  1661.                    gconstpointer    data,
  1662.                    guint        len);
  1663. GArray* g_array_prepend_vals      (GArray       *array,
  1664.                    gconstpointer    data,
  1665.                    guint        len);
  1666. GArray* g_array_insert_vals       (GArray          *array,
  1667.                    guint            index,
  1668.                    gconstpointer    data,
  1669.                    guint            len);
  1670. GArray* g_array_set_size          (GArray       *array,
  1671.                    guint        length);
  1672. GArray* g_array_remove_index      (GArray       *array,
  1673.                    guint        index);
  1674. GArray* g_array_remove_index_fast (GArray       *array,
  1675.                    guint        index);
  1676.  
  1677. /* Resizable pointer array.  This interface is much less complicated
  1678.  * than the above.  Add appends appends a pointer.  Remove fills any
  1679.  * cleared spot and shortens the array. remove_fast will again distort
  1680.  * order.  
  1681.  */
  1682. #define        g_ptr_array_index(array,index) (array->pdata)[index]
  1683. GPtrArray*  g_ptr_array_new           (void);
  1684. void        g_ptr_array_free           (GPtrArray    *array,
  1685.                         gboolean     free_seg);
  1686. void        g_ptr_array_set_size       (GPtrArray    *array,
  1687.                         gint     length);
  1688. gpointer    g_ptr_array_remove_index       (GPtrArray    *array,
  1689.                         guint     index);
  1690. gpointer    g_ptr_array_remove_index_fast  (GPtrArray    *array,
  1691.                         guint     index);
  1692. gboolean    g_ptr_array_remove           (GPtrArray    *array,
  1693.                         gpointer     data);
  1694. gboolean    g_ptr_array_remove_fast        (GPtrArray    *array,
  1695.                         gpointer     data);
  1696. void        g_ptr_array_add           (GPtrArray    *array,
  1697.                         gpointer     data);
  1698.  
  1699. /* Byte arrays, an array of guint8.  Implemented as a GArray,
  1700.  * but type-safe.
  1701.  */
  1702.  
  1703. GByteArray* g_byte_array_new               (void);
  1704. void        g_byte_array_free               (GByteArray     *array,
  1705.                         gboolean      free_segment);
  1706. GByteArray* g_byte_array_append               (GByteArray     *array,
  1707.                         const guint8 *data,
  1708.                         guint      len);
  1709. GByteArray* g_byte_array_prepend           (GByteArray     *array,
  1710.                         const guint8 *data,
  1711.                         guint      len);
  1712. GByteArray* g_byte_array_set_size          (GByteArray     *array,
  1713.                         guint      length);
  1714. GByteArray* g_byte_array_remove_index       (GByteArray     *array,
  1715.                         guint      index);
  1716. GByteArray* g_byte_array_remove_index_fast (GByteArray     *array,
  1717.                         guint      index);
  1718.  
  1719.  
  1720. /* Hash Functions
  1721.  */
  1722. gint  g_str_equal (gconstpointer   v,
  1723.            gconstpointer   v2);
  1724. guint g_str_hash  (gconstpointer   v);
  1725.  
  1726. gint  g_int_equal (gconstpointer   v,
  1727.            gconstpointer   v2);
  1728. guint g_int_hash  (gconstpointer   v);
  1729.  
  1730. /* This "hash" function will just return the key's adress as an
  1731.  * unsigned integer. Useful for hashing on plain adresses or
  1732.  * simple integer values.
  1733.  * passing NULL into g_hash_table_new() as GHashFunc has the
  1734.  * same effect as passing g_direct_hash().
  1735.  */
  1736. guint g_direct_hash  (gconstpointer v);
  1737. gint  g_direct_equal (gconstpointer v,
  1738.               gconstpointer v2);
  1739.  
  1740.  
  1741. /* Quarks (string<->id association)
  1742.  */
  1743. GQuark      g_quark_try_string        (const gchar    *string);
  1744. GQuark      g_quark_from_static_string    (const gchar    *string);
  1745. GQuark      g_quark_from_string        (const gchar    *string);
  1746. gchar*      g_quark_to_string        (GQuark         quark);
  1747.  
  1748.  
  1749. /* Keyed Data List
  1750.  */
  1751. void      g_datalist_init         (GData         **datalist);
  1752. void      g_datalist_clear         (GData         **datalist);
  1753. gpointer  g_datalist_id_get_data     (GData         **datalist,
  1754.                       GQuark       key_id);
  1755. void      g_datalist_id_set_data_full     (GData         **datalist,
  1756.                       GQuark       key_id,
  1757.                       gpointer       data,
  1758.                       GDestroyNotify   destroy_func);
  1759. void      g_datalist_id_remove_no_notify (GData         **datalist,
  1760.                       GQuark       key_id);
  1761. void      g_datalist_foreach         (GData         **datalist,
  1762.                       GDataForeachFunc func,
  1763.                       gpointer       user_data);
  1764. #define      g_datalist_id_set_data(dl, q, d)    \
  1765.      g_datalist_id_set_data_full ((dl), (q), (d), NULL)
  1766. #define      g_datalist_id_remove_data(dl, q)    \
  1767.      g_datalist_id_set_data ((dl), (q), NULL)
  1768. #define      g_datalist_get_data(dl, k)        \
  1769.      (g_datalist_id_get_data ((dl), g_quark_try_string (k)))
  1770. #define      g_datalist_set_data_full(dl, k, d, f)    \
  1771.      g_datalist_id_set_data_full ((dl), g_quark_from_string (k), (d), (f))
  1772. #define      g_datalist_remove_no_notify(dl, k)    \
  1773.      g_datalist_id_remove_no_notify ((dl), g_quark_try_string (k))
  1774. #define      g_datalist_set_data(dl, k, d)        \
  1775.      g_datalist_set_data_full ((dl), (k), (d), NULL)
  1776. #define      g_datalist_remove_data(dl, k)        \
  1777.      g_datalist_id_set_data ((dl), g_quark_try_string (k), NULL)
  1778.  
  1779.  
  1780. /* Location Associated Keyed Data
  1781.  */
  1782. void      g_dataset_destroy        (gconstpointer      dataset_location);
  1783. gpointer  g_dataset_id_get_data        (gconstpointer      dataset_location,
  1784.                      GQuark          key_id);
  1785. void      g_dataset_id_set_data_full    (gconstpointer      dataset_location,
  1786.                      GQuark          key_id,
  1787.                      gpointer      data,
  1788.                      GDestroyNotify      destroy_func);
  1789. void      g_dataset_id_remove_no_notify    (gconstpointer      dataset_location,
  1790.                      GQuark          key_id);
  1791. void      g_dataset_foreach        (gconstpointer      dataset_location,
  1792.                      GDataForeachFunc func,
  1793.                      gpointer      user_data);
  1794. #define      g_dataset_id_set_data(l, k, d)    \
  1795.      g_dataset_id_set_data_full ((l), (k), (d), NULL)
  1796. #define      g_dataset_id_remove_data(l, k)    \
  1797.      g_dataset_id_set_data ((l), (k), NULL)
  1798. #define      g_dataset_get_data(l, k)        \
  1799.      (g_dataset_id_get_data ((l), g_quark_try_string (k)))
  1800. #define      g_dataset_set_data_full(l, k, d, f)    \
  1801.      g_dataset_id_set_data_full ((l), g_quark_from_string (k), (d), (f))
  1802. #define      g_dataset_remove_no_notify(l, k)    \
  1803.      g_dataset_id_remove_no_notify ((l), g_quark_try_string (k))
  1804. #define      g_dataset_set_data(l, k, d)        \
  1805.      g_dataset_set_data_full ((l), (k), (d), NULL)
  1806. #define      g_dataset_remove_data(l, k)        \
  1807.      g_dataset_id_set_data ((l), g_quark_try_string (k), NULL)
  1808.  
  1809.  
  1810. /* GScanner: Flexible lexical scanner for general purpose.
  1811.  */
  1812.  
  1813. /* Character sets */
  1814. #define G_CSET_A_2_Z    "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1815. #define G_CSET_a_2_z    "abcdefghijklmnopqrstuvwxyz"
  1816. #define G_CSET_LATINC    "\300\301\302\303\304\305\306"\
  1817.             "\307\310\311\312\313\314\315\316\317\320"\
  1818.             "\321\322\323\324\325\326"\
  1819.             "\330\331\332\333\334\335\336"
  1820. #define G_CSET_LATINS    "\337\340\341\342\343\344\345\346"\
  1821.             "\347\350\351\352\353\354\355\356\357\360"\
  1822.             "\361\362\363\364\365\366"\
  1823.             "\370\371\372\373\374\375\376\377"
  1824.  
  1825. /* Error types */
  1826. typedef enum
  1827. {
  1828.   G_ERR_UNKNOWN,
  1829.   G_ERR_UNEXP_EOF,
  1830.   G_ERR_UNEXP_EOF_IN_STRING,
  1831.   G_ERR_UNEXP_EOF_IN_COMMENT,
  1832.   G_ERR_NON_DIGIT_IN_CONST,
  1833.   G_ERR_DIGIT_RADIX,
  1834.   G_ERR_FLOAT_RADIX,
  1835.   G_ERR_FLOAT_MALFORMED
  1836. } GErrorType;
  1837.  
  1838. /* Token types */
  1839. typedef enum
  1840. {
  1841.   G_TOKEN_EOF            =   0,
  1842.   
  1843.   G_TOKEN_LEFT_PAREN        = '(',
  1844.   G_TOKEN_RIGHT_PAREN        = ')',
  1845.   G_TOKEN_LEFT_CURLY        = '{',
  1846.   G_TOKEN_RIGHT_CURLY        = '}',
  1847.   G_TOKEN_LEFT_BRACE        = '[',
  1848.   G_TOKEN_RIGHT_BRACE        = ']',
  1849.   G_TOKEN_EQUAL_SIGN        = '=',
  1850.   G_TOKEN_COMMA            = ',',
  1851.   
  1852.   G_TOKEN_NONE            = 256,
  1853.   
  1854.   G_TOKEN_ERROR,
  1855.   
  1856.   G_TOKEN_CHAR,
  1857.   G_TOKEN_BINARY,
  1858.   G_TOKEN_OCTAL,
  1859.   G_TOKEN_INT,
  1860.   G_TOKEN_HEX,
  1861.   G_TOKEN_FLOAT,
  1862.   G_TOKEN_STRING,
  1863.   
  1864.   G_TOKEN_SYMBOL,
  1865.   G_TOKEN_IDENTIFIER,
  1866.   G_TOKEN_IDENTIFIER_NULL,
  1867.   
  1868.   G_TOKEN_COMMENT_SINGLE,
  1869.   G_TOKEN_COMMENT_MULTI,
  1870.   G_TOKEN_LAST
  1871. } GTokenType;
  1872.  
  1873. union    _GTokenValue
  1874. {
  1875.   gpointer    v_symbol;
  1876.   gchar        *v_identifier;
  1877.   gulong    v_binary;
  1878.   gulong    v_octal;
  1879.   gulong    v_int;
  1880.   gdouble    v_float;
  1881.   gulong    v_hex;
  1882.   gchar        *v_string;
  1883.   gchar        *v_comment;
  1884.   guchar    v_char;
  1885.   guint        v_error;
  1886. };
  1887.  
  1888. struct    _GScannerConfig
  1889. {
  1890.   /* Character sets
  1891.    */
  1892.   gchar        *cset_skip_characters;        /* default: " \t\n" */
  1893.   gchar        *cset_identifier_first;
  1894.   gchar        *cset_identifier_nth;
  1895.   gchar        *cpair_comment_single;        /* default: "#\n" */
  1896.   
  1897.   /* Should symbol lookup work case sensitive?
  1898.    */
  1899.   guint        case_sensitive : 1;
  1900.   
  1901.   /* Boolean values to be adjusted "on the fly"
  1902.    * to configure scanning behaviour.
  1903.    */
  1904.   guint        skip_comment_multi : 1;        /* C like comment */
  1905.   guint        skip_comment_single : 1;    /* single line comment */
  1906.   guint        scan_comment_multi : 1;        /* scan multi line comments? */
  1907.   guint        scan_identifier : 1;
  1908.   guint        scan_identifier_1char : 1;
  1909.   guint        scan_identifier_NULL : 1;
  1910.   guint        scan_symbols : 1;
  1911.   guint        scan_binary : 1;
  1912.   guint        scan_octal : 1;
  1913.   guint        scan_float : 1;
  1914.   guint        scan_hex : 1;            /* `0x0ff0' */
  1915.   guint        scan_hex_dollar : 1;        /* `$0ff0' */
  1916.   guint        scan_string_sq : 1;        /* string: 'anything' */
  1917.   guint        scan_string_dq : 1;        /* string: "\\-escapes!\n" */
  1918.   guint        numbers_2_int : 1;        /* bin, octal, hex => int */
  1919.   guint        int_2_float : 1;        /* int => G_TOKEN_FLOAT? */
  1920.   guint        identifier_2_string : 1;
  1921.   guint        char_2_token : 1;        /* return G_TOKEN_CHAR? */
  1922.   guint        symbol_2_token : 1;
  1923.   guint        scope_0_fallback : 1;        /* try scope 0 on lookups? */
  1924. };
  1925.  
  1926. struct    _GScanner
  1927. {
  1928.   /* unused fields */
  1929.   gpointer        user_data;
  1930.   guint            max_parse_errors;
  1931.   
  1932.   /* g_scanner_error() increments this field */
  1933.   guint            parse_errors;
  1934.   
  1935.   /* name of input stream, featured by the default message handler */
  1936.   const gchar        *input_name;
  1937.   
  1938.   /* data pointer for derived structures */
  1939.   gpointer        derived_data;
  1940.   
  1941.   /* link into the scanner configuration */
  1942.   GScannerConfig    *config;
  1943.   
  1944.   /* fields filled in after g_scanner_get_next_token() */
  1945.   GTokenType        token;
  1946.   GTokenValue        value;
  1947.   guint            line;
  1948.   guint            position;
  1949.   
  1950.   /* fields filled in after g_scanner_peek_next_token() */
  1951.   GTokenType        next_token;
  1952.   GTokenValue        next_value;
  1953.   guint            next_line;
  1954.   guint            next_position;
  1955.   
  1956.   /* to be considered private */
  1957.   GHashTable        *symbol_table;
  1958.   gint            input_fd;
  1959.   const gchar        *text;
  1960.   const gchar        *text_end;
  1961.   gchar            *buffer;
  1962.   guint            scope_id;
  1963.   
  1964.   /* handler function for _warn and _error */
  1965.   GScannerMsgFunc    msg_handler;
  1966. };
  1967.  
  1968. GScanner*    g_scanner_new            (GScannerConfig *config_templ);
  1969. void        g_scanner_destroy        (GScanner    *scanner);
  1970. void        g_scanner_input_file        (GScanner    *scanner,
  1971.                          gint        input_fd);
  1972. void        g_scanner_sync_file_offset    (GScanner    *scanner);
  1973. void        g_scanner_input_text        (GScanner    *scanner,
  1974.                          const    gchar    *text,
  1975.                          guint        text_len);
  1976. GTokenType    g_scanner_get_next_token    (GScanner    *scanner);
  1977. GTokenType    g_scanner_peek_next_token    (GScanner    *scanner);
  1978. GTokenType    g_scanner_cur_token        (GScanner    *scanner);
  1979. GTokenValue    g_scanner_cur_value        (GScanner    *scanner);
  1980. guint        g_scanner_cur_line        (GScanner    *scanner);
  1981. guint        g_scanner_cur_position        (GScanner    *scanner);
  1982. gboolean    g_scanner_eof            (GScanner    *scanner);
  1983. guint        g_scanner_set_scope        (GScanner    *scanner,
  1984.                          guint         scope_id);
  1985. void        g_scanner_scope_add_symbol    (GScanner    *scanner,
  1986.                          guint         scope_id,
  1987.                          const gchar    *symbol,
  1988.                          gpointer    value);
  1989. void        g_scanner_scope_remove_symbol    (GScanner    *scanner,
  1990.                          guint         scope_id,
  1991.                          const gchar    *symbol);
  1992. gpointer    g_scanner_scope_lookup_symbol    (GScanner    *scanner,
  1993.                          guint         scope_id,
  1994.                          const gchar    *symbol);
  1995. void        g_scanner_scope_foreach_symbol    (GScanner    *scanner,
  1996.                          guint         scope_id,
  1997.                          GHFunc         func,
  1998.                          gpointer     user_data);
  1999. gpointer    g_scanner_lookup_symbol        (GScanner    *scanner,
  2000.                          const gchar    *symbol);
  2001. void        g_scanner_freeze_symbol_table    (GScanner    *scanner);
  2002. void        g_scanner_thaw_symbol_table    (GScanner    *scanner);
  2003. void        g_scanner_unexp_token        (GScanner    *scanner,
  2004.                          GTokenType    expected_token,
  2005.                          const gchar    *identifier_spec,
  2006.                          const gchar    *symbol_spec,
  2007.                          const gchar    *symbol_name,
  2008.                          const gchar    *message,
  2009.                          gint         is_error);
  2010. void        g_scanner_error            (GScanner    *scanner,
  2011.                          const gchar    *format,
  2012.                          ...) G_GNUC_PRINTF (2,3);
  2013. void        g_scanner_warn            (GScanner    *scanner,
  2014.                          const gchar    *format,
  2015.                          ...) G_GNUC_PRINTF (2,3);
  2016. gint        g_scanner_stat_mode        (const gchar    *filename);
  2017. /* keep downward source compatibility */
  2018. #define        g_scanner_add_symbol( scanner, symbol, value )    G_STMT_START { \
  2019.   g_scanner_scope_add_symbol ((scanner), 0, (symbol), (value)); \
  2020. } G_STMT_END
  2021. #define        g_scanner_remove_symbol( scanner, symbol )    G_STMT_START { \
  2022.   g_scanner_scope_remove_symbol ((scanner), 0, (symbol)); \
  2023. } G_STMT_END
  2024. #define        g_scanner_foreach_symbol( scanner, func, data )    G_STMT_START { \
  2025.   g_scanner_scope_foreach_symbol ((scanner), 0, (func), (data)); \
  2026. } G_STMT_END
  2027.  
  2028.  
  2029. /* GCompletion
  2030.  */
  2031.  
  2032. struct _GCompletion
  2033. {
  2034.   GList* items;
  2035.   GCompletionFunc func;
  2036.   
  2037.   gchar* prefix;
  2038.   GList* cache;
  2039. };
  2040.  
  2041. GCompletion* g_completion_new           (GCompletionFunc func);
  2042. void         g_completion_add_items    (GCompletion*    cmp,
  2043.                     GList*        items);
  2044. void         g_completion_remove_items (GCompletion*    cmp,
  2045.                     GList*        items);
  2046. void         g_completion_clear_items  (GCompletion*    cmp);
  2047. GList*         g_completion_complete     (GCompletion*    cmp,
  2048.                     gchar*        prefix,
  2049.                     gchar**        new_prefix);
  2050. void         g_completion_free           (GCompletion*    cmp);
  2051.  
  2052.  
  2053. /* GDate
  2054.  *
  2055.  * Date calculations (not time for now, to be resolved). These are a
  2056.  * mutant combination of Steffen Beyer's DateCalc routines
  2057.  * (http://www.perl.com/CPAN/authors/id/STBEY/) and Jon Trowbridge's
  2058.  * date routines (written for in-house software).  Written by Havoc
  2059.  * Pennington <hp@pobox.com> 
  2060.  */
  2061.  
  2062. typedef guint16 GDateYear;
  2063. typedef guint8  GDateDay;   /* day of the month */
  2064. typedef struct _GDate GDate;
  2065. /* make struct tm known without having to include time.h */
  2066. struct tm;
  2067.  
  2068. /* enum used to specify order of appearance in parsed date strings */
  2069. typedef enum
  2070. {
  2071.   G_DATE_DAY   = 0,
  2072.   G_DATE_MONTH = 1,
  2073.   G_DATE_YEAR  = 2
  2074. } GDateDMY;
  2075.  
  2076. /* actual week and month values */
  2077. typedef enum
  2078. {
  2079.   G_DATE_BAD_WEEKDAY  = 0,
  2080.   G_DATE_MONDAY       = 1,
  2081.   G_DATE_TUESDAY      = 2,
  2082.   G_DATE_WEDNESDAY    = 3,
  2083.   G_DATE_THURSDAY     = 4,
  2084.   G_DATE_FRIDAY       = 5,
  2085.   G_DATE_SATURDAY     = 6,
  2086.   G_DATE_SUNDAY       = 7
  2087. } GDateWeekday;
  2088. typedef enum
  2089. {
  2090.   G_DATE_BAD_MONTH = 0,
  2091.   G_DATE_JANUARY   = 1,
  2092.   G_DATE_FEBRUARY  = 2,
  2093.   G_DATE_MARCH     = 3,
  2094.   G_DATE_APRIL     = 4,
  2095.   G_DATE_MAY       = 5,
  2096.   G_DATE_JUNE      = 6,
  2097.   G_DATE_JULY      = 7,
  2098.   G_DATE_AUGUST    = 8,
  2099.   G_DATE_SEPTEMBER = 9,
  2100.   G_DATE_OCTOBER   = 10,
  2101.   G_DATE_NOVEMBER  = 11,
  2102.   G_DATE_DECEMBER  = 12
  2103. } GDateMonth;
  2104.  
  2105. #define G_DATE_BAD_JULIAN 0U
  2106. #define G_DATE_BAD_DAY    0U
  2107. #define G_DATE_BAD_YEAR   0U
  2108.  
  2109. /* Note: directly manipulating structs is generally a bad idea, but
  2110.  * in this case it's an *incredibly* bad idea, because all or part
  2111.  * of this struct can be invalid at any given time. Use the functions,
  2112.  * or you will get hosed, I promise.
  2113.  */
  2114. struct _GDate
  2115.   guint julian_days : 32; /* julian days representation - we use a
  2116.                            *  bitfield hoping that 64 bit platforms
  2117.                            *  will pack this whole struct in one big
  2118.                            *  int 
  2119.                            */
  2120.  
  2121.   guint julian : 1;    /* julian is valid */
  2122.   guint dmy    : 1;    /* dmy is valid */
  2123.  
  2124.   /* DMY representation */
  2125.   guint day    : 6;  
  2126.   guint month  : 4; 
  2127.   guint year   : 16; 
  2128. };
  2129.  
  2130. /* g_date_new() returns an invalid date, you then have to _set() stuff 
  2131.  * to get a usable object. You can also allocate a GDate statically,
  2132.  * then call g_date_clear() to initialize.
  2133.  */
  2134. GDate*       g_date_new                   (void);
  2135. GDate*       g_date_new_dmy               (GDateDay     day, 
  2136.                                            GDateMonth   month, 
  2137.                                            GDateYear    year);
  2138. GDate*       g_date_new_julian            (guint32      julian_day);
  2139. void         g_date_free                  (GDate       *date);
  2140.  
  2141. /* check g_date_valid() after doing an operation that might fail, like
  2142.  * _parse.  Almost all g_date operations are undefined on invalid
  2143.  * dates (the exceptions are the mutators, since you need those to
  2144.  * return to validity).  
  2145.  */
  2146. gboolean     g_date_valid                 (GDate       *date);
  2147. gboolean     g_date_valid_day             (GDateDay     day);
  2148. gboolean     g_date_valid_month           (GDateMonth   month);
  2149. gboolean     g_date_valid_year            (GDateYear    year);
  2150. gboolean     g_date_valid_weekday         (GDateWeekday weekday);
  2151. gboolean     g_date_valid_julian          (guint32      julian_date);
  2152. gboolean     g_date_valid_dmy             (GDateDay     day,
  2153.                                            GDateMonth   month,
  2154.                                            GDateYear    year);
  2155.  
  2156. GDateWeekday g_date_weekday               (GDate       *date);
  2157. GDateMonth   g_date_month                 (GDate       *date);
  2158. GDateYear    g_date_year                  (GDate       *date);
  2159. GDateDay     g_date_day                   (GDate       *date);
  2160. guint32      g_date_julian                (GDate       *date);
  2161. guint        g_date_day_of_year           (GDate       *date);
  2162.  
  2163. /* First monday/sunday is the start of week 1; if we haven't reached
  2164.  * that day, return 0. These are not ISO weeks of the year; that
  2165.  * routine needs to be added.
  2166.  * these functions return the number of weeks, starting on the
  2167.  * corrsponding day
  2168.  */
  2169. guint        g_date_monday_week_of_year   (GDate      *date);
  2170. guint        g_date_sunday_week_of_year   (GDate      *date);
  2171.  
  2172. /* If you create a static date struct you need to clear it to get it
  2173.  * in a sane state before use. You can clear a whole array at
  2174.  * once with the ndates argument.
  2175.  */
  2176. void         g_date_clear                 (GDate       *date, 
  2177.                                            guint        n_dates);
  2178.  
  2179. /* The parse routine is meant for dates typed in by a user, so it
  2180.  * permits many formats but tries to catch common typos. If your data
  2181.  * needs to be strictly validated, it is not an appropriate function.
  2182.  */
  2183. void         g_date_set_parse             (GDate       *date,
  2184.                                            const gchar *str);
  2185. void         g_date_set_time              (GDate       *date, 
  2186.                                            GTime        time);
  2187. void         g_date_set_month             (GDate       *date, 
  2188.                                            GDateMonth   month);
  2189. void         g_date_set_day               (GDate       *date, 
  2190.                                            GDateDay     day);
  2191. void         g_date_set_year              (GDate       *date,
  2192.                                            GDateYear    year);
  2193. void         g_date_set_dmy               (GDate       *date,
  2194.                                            GDateDay     day,
  2195.                                            GDateMonth   month,
  2196.                                            GDateYear    y);
  2197. void         g_date_set_julian            (GDate       *date,
  2198.                                            guint32      julian_date);
  2199. gboolean     g_date_is_first_of_month     (GDate       *date);
  2200. gboolean     g_date_is_last_of_month      (GDate       *date);
  2201.  
  2202. /* To go forward by some number of weeks just go forward weeks*7 days */
  2203. void         g_date_add_days              (GDate       *date, 
  2204.                                            guint        n_days);
  2205. void         g_date_subtract_days         (GDate       *date, 
  2206.                                            guint        n_days);
  2207.  
  2208. /* If you add/sub months while day > 28, the day might change */
  2209. void         g_date_add_months            (GDate       *date,
  2210.                                            guint        n_months);
  2211. void         g_date_subtract_months       (GDate       *date,
  2212.                                            guint        n_months);
  2213.  
  2214. /* If it's feb 29, changing years can move you to the 28th */
  2215. void         g_date_add_years             (GDate       *date,
  2216.                                            guint        n_years);
  2217. void         g_date_subtract_years        (GDate       *date,
  2218.                                            guint        n_years);
  2219. gboolean     g_date_is_leap_year          (GDateYear    year);
  2220. guint8       g_date_days_in_month         (GDateMonth   month, 
  2221.                                            GDateYear    year);
  2222. guint8       g_date_monday_weeks_in_year  (GDateYear    year);
  2223. guint8       g_date_sunday_weeks_in_year  (GDateYear    year);
  2224.  
  2225. /* qsort-friendly (with a cast...) */
  2226. gint         g_date_compare               (GDate       *lhs,
  2227.                                            GDate       *rhs);
  2228. void         g_date_to_struct_tm          (GDate       *date,
  2229.                                            struct tm   *tm);
  2230.  
  2231. /* Just like strftime() except you can only use date-related formats.
  2232.  *   Using a time format is undefined.
  2233.  */
  2234. gsize        g_date_strftime              (gchar       *s,
  2235.                                            gsize        slen,
  2236.                                            const gchar *format,
  2237.                                            GDate       *date);
  2238.  
  2239.  
  2240. /* GRelation
  2241.  *
  2242.  * Indexed Relations.  Imagine a really simple table in a
  2243.  * database.  Relations are not ordered.  This data type is meant for
  2244.  * maintaining a N-way mapping.
  2245.  *
  2246.  * g_relation_new() creates a relation with FIELDS fields
  2247.  *
  2248.  * g_relation_destroy() frees all resources
  2249.  * g_tuples_destroy() frees the result of g_relation_select()
  2250.  *
  2251.  * g_relation_index() indexes relation FIELD with the provided
  2252.  *   equality and hash functions.  this must be done before any
  2253.  *   calls to insert are made.
  2254.  *
  2255.  * g_relation_insert() inserts a new tuple.  you are expected to
  2256.  *   provide the right number of fields.
  2257.  *
  2258.  * g_relation_delete() deletes all relations with KEY in FIELD
  2259.  * g_relation_select() returns ...
  2260.  * g_relation_count() counts ...
  2261.  */
  2262.  
  2263. GRelation* g_relation_new     (gint        fields);
  2264. void       g_relation_destroy (GRelation   *relation);
  2265. void       g_relation_index   (GRelation   *relation,
  2266.                    gint        field,
  2267.                    GHashFunc    hash_func,
  2268.                    GCompareFunc key_compare_func);
  2269. void       g_relation_insert  (GRelation   *relation,
  2270.                    ...);
  2271. gint       g_relation_delete  (GRelation   *relation,
  2272.                    gconstpointer  key,
  2273.                    gint        field);
  2274. GTuples*   g_relation_select  (GRelation   *relation,
  2275.                    gconstpointer  key,
  2276.                    gint        field);
  2277. gint       g_relation_count   (GRelation   *relation,
  2278.                    gconstpointer  key,
  2279.                    gint        field);
  2280. gboolean   g_relation_exists  (GRelation   *relation,
  2281.                    ...);
  2282. void       g_relation_print   (GRelation   *relation);
  2283.  
  2284. void       g_tuples_destroy   (GTuples       *tuples);
  2285. gpointer   g_tuples_index     (GTuples       *tuples,
  2286.                    gint        index,
  2287.                    gint        field);
  2288.  
  2289.  
  2290. /* Prime numbers.
  2291.  */
  2292.  
  2293. /* This function returns prime numbers spaced by approximately 1.5-2.0
  2294.  * and is for use in resizing data structures which prefer
  2295.  * prime-valued sizes.    The closest spaced prime function returns the
  2296.  * next largest prime, or the highest it knows about which is about
  2297.  * MAXINT/4.
  2298.  */
  2299. guint       g_spaced_primes_closest (guint num);
  2300.  
  2301.  
  2302. /* GIOChannel
  2303.  */
  2304.  
  2305. typedef struct _GIOFuncs GIOFuncs;
  2306. typedef enum
  2307. {
  2308.   G_IO_ERROR_NONE,
  2309.   G_IO_ERROR_AGAIN,
  2310.   G_IO_ERROR_INVAL,
  2311.   G_IO_ERROR_UNKNOWN
  2312. } GIOError;
  2313. typedef enum
  2314. {
  2315.   G_SEEK_CUR,
  2316.   G_SEEK_SET,
  2317.   G_SEEK_END
  2318. } GSeekType;
  2319. typedef enum
  2320. {
  2321.   G_IO_IN    GLIB_SYSDEF_POLLIN,
  2322.   G_IO_OUT    GLIB_SYSDEF_POLLOUT,
  2323.   G_IO_PRI    GLIB_SYSDEF_POLLPRI,
  2324.   G_IO_ERR    GLIB_SYSDEF_POLLERR,
  2325.   G_IO_HUP    GLIB_SYSDEF_POLLHUP,
  2326.   G_IO_NVAL    GLIB_SYSDEF_POLLNVAL
  2327. } GIOCondition;
  2328.  
  2329. struct _GIOChannel
  2330. {
  2331.   guint channel_flags;
  2332.   guint ref_count;
  2333.   GIOFuncs *funcs;
  2334. };
  2335.  
  2336. typedef gboolean (*GIOFunc) (GIOChannel   *source,
  2337.                  GIOCondition  condition,
  2338.                  gpointer      data);
  2339. struct _GIOFuncs
  2340. {
  2341.   GIOError (*io_read)   (GIOChannel     *channel, 
  2342.                  gchar          *buf, 
  2343.                  guint           count,
  2344.              guint          *bytes_read);
  2345.   GIOError (*io_write)  (GIOChannel     *channel, 
  2346.               gchar          *buf, 
  2347.              guint           count,
  2348.              guint          *bytes_written);
  2349.   GIOError (*io_seek)   (GIOChannel       *channel, 
  2350.               gint            offset, 
  2351.                GSeekType       type);
  2352.   void (*io_close)      (GIOChannel    *channel);
  2353.   guint (*io_add_watch) (GIOChannel     *channel,
  2354.              gint            priority,
  2355.              GIOCondition    condition,
  2356.              GIOFunc         func,
  2357.              gpointer        user_data,
  2358.              GDestroyNotify  notify);
  2359.   void (*io_free)       (GIOChannel    *channel);
  2360. };
  2361.  
  2362. void        g_io_channel_init   (GIOChannel    *channel);
  2363. void        g_io_channel_ref    (GIOChannel    *channel);
  2364. void        g_io_channel_unref  (GIOChannel    *channel);
  2365. GIOError    g_io_channel_read   (GIOChannel    *channel, 
  2366.                      gchar         *buf, 
  2367.                      guint          count,
  2368.                      guint         *bytes_read);
  2369. GIOError  g_io_channel_write    (GIOChannel    *channel, 
  2370.                      gchar         *buf, 
  2371.                      guint          count,
  2372.                      guint         *bytes_written);
  2373. GIOError  g_io_channel_seek     (GIOChannel    *channel,
  2374.                      gint           offset, 
  2375.                      GSeekType      type);
  2376. void      g_io_channel_close    (GIOChannel    *channel);
  2377. guint     g_io_add_watch_full   (GIOChannel    *channel,
  2378.                      gint           priority,
  2379.                      GIOCondition   condition,
  2380.                      GIOFunc        func,
  2381.                      gpointer       user_data,
  2382.                      GDestroyNotify notify);
  2383. guint    g_io_add_watch         (GIOChannel    *channel,
  2384.                      GIOCondition   condition,
  2385.                      GIOFunc        func,
  2386.                      gpointer       user_data);
  2387.  
  2388.  
  2389. /* Main loop
  2390.  */
  2391. typedef struct _GTimeVal    GTimeVal;
  2392. typedef struct _GSourceFuncs    GSourceFuncs;
  2393. typedef struct _GMainLoop    GMainLoop;    /* Opaque */
  2394.  
  2395. struct _GTimeVal
  2396. {
  2397.   glong tv_sec;
  2398.   glong tv_usec;
  2399. };
  2400. struct _GSourceFuncs
  2401. {
  2402.   gboolean (*prepare)  (gpointer  source_data, 
  2403.             GTimeVal *current_time,
  2404.             gint     *timeout,
  2405.             gpointer  user_data);
  2406.   gboolean (*check)    (gpointer  source_data,
  2407.             GTimeVal *current_time,
  2408.             gpointer  user_data);
  2409.   gboolean (*dispatch) (gpointer  source_data, 
  2410.             GTimeVal *current_time,
  2411.             gpointer  user_data);
  2412.   GDestroyNotify destroy;
  2413. };
  2414.  
  2415. /* Standard priorities */
  2416.  
  2417. #define G_PRIORITY_HIGH            -100
  2418. #define G_PRIORITY_DEFAULT          0
  2419. #define G_PRIORITY_HIGH_IDLE        100
  2420. #define G_PRIORITY_DEFAULT_IDLE     200
  2421. #define G_PRIORITY_LOW                300
  2422.  
  2423. typedef gboolean (*GSourceFunc) (gpointer data);
  2424.  
  2425. /* Hooks for adding to the main loop */
  2426. guint    g_source_add                        (gint           priority, 
  2427.                           gboolean       can_recurse,
  2428.                           GSourceFuncs  *funcs,
  2429.                           gpointer       source_data, 
  2430.                           gpointer       user_data,
  2431.                           GDestroyNotify notify);
  2432. gboolean g_source_remove                     (guint          tag);
  2433. gboolean g_source_remove_by_user_data        (gpointer       user_data);
  2434. gboolean g_source_remove_by_source_data      (gpointer       source_data);
  2435. gboolean g_source_remove_by_funcs_user_data  (GSourceFuncs  *funcs,
  2436.                           gpointer       user_data);
  2437.  
  2438. void g_get_current_time            (GTimeVal       *result);
  2439.  
  2440. /* Running the main loop */
  2441. GMainLoop*    g_main_new        (gboolean     is_running);
  2442. void        g_main_run        (GMainLoop    *loop);
  2443. void        g_main_quit        (GMainLoop    *loop);
  2444. void        g_main_destroy        (GMainLoop    *loop);
  2445. gboolean    g_main_is_running    (GMainLoop    *loop);
  2446.  
  2447. /* Run a single iteration of the mainloop. If block is FALSE,
  2448.  * will never block
  2449.  */
  2450. gboolean    g_main_iteration    (gboolean    may_block);
  2451.  
  2452. /* See if any events are pending */
  2453. gboolean    g_main_pending        (void);
  2454.  
  2455. /* Idles and timeouts */
  2456. guint        g_timeout_add_full    (gint           priority,
  2457.                      guint          interval, 
  2458.                      GSourceFunc    function,
  2459.                      gpointer       data,
  2460.                      GDestroyNotify notify);
  2461. guint        g_timeout_add        (guint          interval,
  2462.                      GSourceFunc    function,
  2463.                      gpointer       data);
  2464. guint        g_idle_add           (GSourceFunc    function,
  2465.                      gpointer    data);
  2466. guint           g_idle_add_full        (gint       priority,
  2467.                      GSourceFunc    function,
  2468.                      gpointer    data,
  2469.                      GDestroyNotify destroy);
  2470. gboolean    g_idle_remove_by_data    (gpointer    data);
  2471.  
  2472. /* GPollFD
  2473.  *
  2474.  * System-specific IO and main loop calls
  2475.  *
  2476.  * On Win32, the fd in a GPollFD should be Win32 HANDLE (*not* a file
  2477.  * descriptor as provided by the C runtime) that can be used by
  2478.  * MsgWaitForMultipleObjects. This does *not* include file handles
  2479.  * from CreateFile, SOCKETs, nor pipe handles. (But you can use
  2480.  * WSAEventSelect to signal events when a SOCKET is readable).
  2481.  *
  2482.  * On Win32, fd can also be the special value G_WIN32_MSG_HANDLE to
  2483.  * indicate polling for messages. These message queue GPollFDs should
  2484.  * be added with the g_main_poll_win32_msg_add function.
  2485.  *
  2486.  * But note that G_WIN32_MSG_HANDLE GPollFDs should not be used by GDK
  2487.  * (GTK) programs, as GDK itself wants to read messages and convert them
  2488.  * to GDK events.
  2489.  *
  2490.  * So, unless you really know what you are doing, it's best not to try
  2491.  * to use the main loop polling stuff for your own needs on
  2492.  * Win32. It's really only written for the GIMP's needs so
  2493.  * far.
  2494.  */
  2495.  
  2496. typedef struct _GPollFD GPollFD;
  2497. typedef gint    (*GPollFunc)    (GPollFD *ufds,
  2498.                  guint      nfsd,
  2499.                  gint     timeout);
  2500. struct _GPollFD
  2501. {
  2502.   gint        fd;
  2503.   gushort     events;
  2504.   gushort     revents;
  2505. };
  2506.  
  2507. void        g_main_add_poll          (GPollFD    *fd,
  2508.                       gint        priority);
  2509. void        g_main_remove_poll       (GPollFD    *fd);
  2510. void        g_main_set_poll_func     (GPollFunc   func);
  2511.  
  2512. /* On Unix, IO channels created with this function for any file
  2513.  * descriptor or socket.
  2514.  *
  2515.  * On Win32, use this only for plain files opened with the MSVCRT (the
  2516.  * Microsoft run-time C library) _open(), including file descriptors
  2517.  * 0, 1 and 2 (corresponding to stdin, stdout and stderr).
  2518.  * Actually, don't do even that, this code isn't done yet.
  2519.  *
  2520.  * The term file descriptor as used in the context of Win32 refers to
  2521.  * the emulated Unix-like file descriptors MSVCRT provides.
  2522.  */
  2523. GIOChannel* g_io_channel_unix_new    (int         fd);
  2524. gint        g_io_channel_unix_get_fd (GIOChannel *channel);
  2525.  
  2526. #ifdef NATIVE_WIN32
  2527.  
  2528. GUTILS_C_VAR guint g_pipe_readable_msg;
  2529.  
  2530. #define G_WIN32_MSG_HANDLE 19981206
  2531.  
  2532. /* This is used to add polling for Windows messages. GDK (GTk+) programs
  2533.  * should *not* use this. (In fact, I can't think of any program that
  2534.  * would want to use this, but it's here just for completeness's sake.
  2535.  */
  2536. void        g_main_poll_win32_msg_add(gint        priority,
  2537.                       GPollFD    *fd,
  2538.                       guint       hwnd);
  2539.  
  2540. /* An IO channel for Windows messages for window handle hwnd. */
  2541. GIOChannel *g_io_channel_win32_new_messages (guint hwnd);
  2542.  
  2543. /* An IO channel for an anonymous pipe as returned from the MSVCRT
  2544.  * _pipe(), with no mechanism for the writer to tell the reader when
  2545.  * there is data in the pipe.
  2546.  *
  2547.  * This is not really implemented yet.
  2548.  */
  2549. GIOChannel *g_io_channel_win32_new_pipe (int fd);
  2550.  
  2551. /* An IO channel for a pipe as returned from the MSVCRT _pipe(), with
  2552.  * Windows user messages used to signal data in the pipe for the
  2553.  * reader.
  2554.  *
  2555.  * fd is the file descriptor. For the write end, peer is the thread id
  2556.  * of the reader, and peer_fd is his file descriptor for the read end
  2557.  * of the pipe.
  2558.  *
  2559.  * This is used by the GIMP, and works.
  2560.  */
  2561. GIOChannel *g_io_channel_win32_new_pipe_with_wakeups (int   fd,
  2562.                               guint peer,
  2563.                               int   peer_fd);
  2564.  
  2565. void        g_io_channel_win32_pipe_request_wakeups (GIOChannel *channel,
  2566.                              guint       peer,
  2567.                              int         peer_fd);
  2568.  
  2569. void        g_io_channel_win32_pipe_readable (int   fd,
  2570.                           guint offset);
  2571.  
  2572. /* Get the C runtime file descriptor of a channel. */
  2573. gint        g_io_channel_win32_get_fd (GIOChannel *channel);
  2574.  
  2575. /* An IO channel for a SOCK_STREAM winsock socket. The parameter is
  2576.  * actually a SOCKET.
  2577.  */
  2578. GIOChannel *g_io_channel_win32_new_stream_socket (int socket);
  2579.  
  2580. #endif
  2581.  
  2582. /* Windows emulation stubs for common Unix functions
  2583.  */
  2584. #ifdef NATIVE_WIN32
  2585. #  define MAXPATHLEN 1024
  2586. #  ifdef _MSC_VER
  2587. typedef int pid_t;
  2588.  
  2589. /* These POSIXish functions are available in the Microsoft C library
  2590.  * prefixed with underscore (which of course technically speaking is
  2591.  * the Right Thing, as they are non-ANSI. Not that being non-ANSI
  2592.  * prevents Microsoft from practically requiring you to include
  2593.  * <windows.h> every now and then...).
  2594.  *
  2595.  * You still need to include the appropriate headers to get the
  2596.  * prototypes, <io.h> or <direct.h>.
  2597.  *
  2598.  * For some functions, we provide emulators in glib, which are prefixed
  2599.  * with gwin_.
  2600.  */
  2601. #    define getcwd        _getcwd
  2602. #    define getpid        _getpid
  2603. #    define access        _access
  2604. #    define open        _open
  2605. #    define read        _read
  2606. #    define write        _write
  2607. #    define lseek        _lseek
  2608. #    define close        _close
  2609. #    define pipe(phandles)    _pipe (phandles, 4096, _O_BINARY)
  2610. #    define popen        _popen
  2611. #    define pclose        _pclose
  2612. #    define fdopen        _fdopen
  2613. #    define ftruncate(fd, size)    gwin_ftruncate (fd, size)
  2614. #    define opendir        gwin_opendir
  2615. #    define readdir        gwin_readdir
  2616. #    define rewinddir        gwin_rewinddir
  2617. #    define closedir        gwin_closedir
  2618. #    define NAME_MAX 255
  2619. struct DIR
  2620. {
  2621.   gchar    *dir_name;
  2622.   gboolean  just_opened;
  2623.   guint     find_file_handle;
  2624.   gpointer  find_file_data;
  2625. };
  2626. typedef struct DIR DIR;
  2627. struct dirent
  2628. {
  2629.   gchar  d_name[NAME_MAX + 1];
  2630. };
  2631. /* emulation functions */
  2632. extern int    gwin_ftruncate    (gint         f,
  2633.                  guint         size);
  2634. DIR*        gwin_opendir    (const gchar    *dirname);
  2635. struct dirent*    gwin_readdir      (DIR        *dir);
  2636. void        gwin_rewinddir     (DIR        *dir);
  2637. gint        gwin_closedir      (DIR        *dir);
  2638. #  endif /* _MSC_VER */
  2639. #endif     /* NATIVE_WIN32 */
  2640.  
  2641.  
  2642. /* GLib Thread support
  2643.  */
  2644. typedef struct _GMutex        GMutex;
  2645. typedef struct _GCond        GCond;
  2646. typedef struct _GPrivate    GPrivate;
  2647. typedef struct _GStaticPrivate    GStaticPrivate;
  2648. typedef struct _GThreadFunctions GThreadFunctions;
  2649. struct _GThreadFunctions
  2650. {
  2651.   GMutex*  (*mutex_new)       (void);
  2652.   void     (*mutex_lock)      (GMutex        *mutex);
  2653.   gboolean (*mutex_trylock)   (GMutex        *mutex);
  2654.   void     (*mutex_unlock)    (GMutex        *mutex);
  2655.   void     (*mutex_free)      (GMutex        *mutex);
  2656.   GCond*   (*cond_new)        (void);
  2657.   void     (*cond_signal)     (GCond        *cond);
  2658.   void     (*cond_broadcast)  (GCond        *cond);
  2659.   void     (*cond_wait)       (GCond        *cond,
  2660.                    GMutex        *mutex);
  2661.   gboolean (*cond_timed_wait) (GCond        *cond,
  2662.                    GMutex        *mutex, 
  2663.                    GTimeVal     *end_time);
  2664.   void      (*cond_free)      (GCond        *cond);
  2665.   GPrivate* (*private_new)    (GDestroyNotify     destructor);
  2666.   gpointer  (*private_get)    (GPrivate        *private_key);
  2667.   void      (*private_set)    (GPrivate        *private_key,
  2668.                    gpointer         data);
  2669. };
  2670.  
  2671. GUTILS_C_VAR GThreadFunctions    g_thread_functions_for_glib_use;
  2672. GUTILS_C_VAR gboolean        g_thread_use_default_impl;
  2673. GUTILS_C_VAR gboolean        g_threads_got_initialized;
  2674.  
  2675. /* initializes the mutex/cond/private implementation for glib, might
  2676.  * only be called once, and must not be called directly or indirectly
  2677.  * from another glib-function, e.g. as a callback.
  2678.  */
  2679. void    g_thread_init    (GThreadFunctions    *vtable);
  2680.  
  2681. /* internal function for fallback static mutex implementation */
  2682. GMutex*    g_static_mutex_get_mutex_impl    (GMutex    **mutex);
  2683.  
  2684. /* shorthands for conditional and unconditional function calls */
  2685. #define G_THREAD_UF(name, arglist) \
  2686.     (*g_thread_functions_for_glib_use . name) arglist
  2687. #define G_THREAD_CF(name, fail, arg) \
  2688.     (g_thread_supported () ? G_THREAD_UF (name, arg) : (fail))
  2689. /* keep in mind, all those mutexes and static mutexes are not 
  2690.  * recursive in general, don't rely on that
  2691.  */
  2692. #define    g_thread_supported()    (g_threads_got_initialized)
  2693. #define g_mutex_new()            G_THREAD_UF (mutex_new,      ())
  2694. #define g_mutex_lock(mutex)      G_THREAD_CF (mutex_lock,     (void)0, (mutex))
  2695. #define g_mutex_trylock(mutex)   G_THREAD_CF (mutex_trylock,  TRUE,    (mutex))
  2696. #define g_mutex_unlock(mutex)    G_THREAD_CF (mutex_unlock,   (void)0, (mutex))
  2697. #define g_mutex_free(mutex)      G_THREAD_CF (mutex_free,     (void)0, (mutex))
  2698. #define g_cond_new()             G_THREAD_UF (cond_new,       ())
  2699. #define g_cond_signal(cond)      G_THREAD_CF (cond_signal,    (void)0, (cond))
  2700. #define g_cond_broadcast(cond)   G_THREAD_CF (cond_broadcast, (void)0, (cond))
  2701. #define g_cond_wait(cond, mutex) G_THREAD_CF (cond_wait,      (void)0, (cond, \
  2702.                                                                         mutex))
  2703. #define g_cond_free(cond)        G_THREAD_CF (cond_free,      (void)0, (cond))
  2704. #define g_cond_timed_wait(cond, mutex, abs_time) G_THREAD_CF (cond_timed_wait, \
  2705.                                                               TRUE, \
  2706.                                                               (cond, mutex, \
  2707.                                    abs_time))
  2708. #define g_private_new(destructor)      G_THREAD_UF (private_new, (destructor))
  2709. #define g_private_get(private_key)      G_THREAD_CF (private_get, \
  2710.                                                        ((gpointer)private_key), \
  2711.                                                        (private_key))
  2712. #define g_private_set(private_key, value) G_THREAD_CF (private_set, \
  2713.                                                        (void) (private_key = \
  2714.                                                         (GPrivate*) (value)), \
  2715.                                                        (private_key, value))
  2716. /* GStaticMutexes can be statically initialized with the value
  2717.  * G_STATIC_MUTEX_INIT, and then they can directly be used, that is
  2718.  * much easier, than having to explicitly allocate the mutex before
  2719.  * use
  2720.  */
  2721. #define g_static_mutex_lock(mutex) \
  2722.     g_mutex_lock (g_static_mutex_get_mutex (mutex))
  2723. #define g_static_mutex_trylock(mutex) \
  2724.     g_mutex_trylock (g_static_mutex_get_mutex (mutex))
  2725. #define g_static_mutex_unlock(mutex) \
  2726.     g_mutex_unlock (g_static_mutex_get_mutex (mutex)) 
  2727. struct _GStaticPrivate
  2728. {
  2729.   guint index;
  2730. };
  2731. #define G_STATIC_PRIVATE_INIT { 0 }
  2732. gpointer g_static_private_get (GStaticPrivate    *private_key);
  2733. void     g_static_private_set (GStaticPrivate    *private_key, 
  2734.                    gpointer             data,
  2735.                    GDestroyNotify    notify);
  2736.  
  2737. /* these are some convenience macros that expand to nothing if GLib
  2738.  * was configured with --disable-threads. for using StaticMutexes,
  2739.  * you define them with G_LOCK_DEFINE_STATIC (name) or G_LOCK_DEFINE (name)
  2740.  * if you need to export the mutex. With G_LOCK_EXTERN (name) you can
  2741.  * declare such an globally defined lock. name is a unique identifier
  2742.  * for the protected varibale or code portion. locking, testing and
  2743.  * unlocking of such mutexes can be done with G_LOCK(), G_UNLOCK() and
  2744.  * G_TRYLOCK() respectively.  
  2745.  */
  2746. extern void glib_dummy_decl (void);
  2747. #define G_LOCK_NAME(name)        (g__ ## name ## _lock)
  2748. #ifdef    G_THREADS_ENABLED
  2749. #  define G_LOCK_DEFINE_STATIC(name)    static G_LOCK_DEFINE (name)
  2750. #  define G_LOCK_DEFINE(name)        \
  2751.     GStaticMutex G_LOCK_NAME (name) = G_STATIC_MUTEX_INIT 
  2752. #  define G_LOCK_EXTERN(name)        extern GStaticMutex G_LOCK_NAME (name)
  2753.  
  2754. #  ifdef G_DEBUG_LOCKS
  2755. #    define G_LOCK(name)        G_STMT_START{          \
  2756.         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,              \
  2757.            "file %s: line %d (%s): locking: %s ",              \
  2758.            __FILE__,    __LINE__, G_GNUC_PRETTY_FUNCTION, \
  2759.                #name);                                            \
  2760.         g_static_mutex_lock (&G_LOCK_NAME (name));                \
  2761.      }G_STMT_END
  2762. #    define G_UNLOCK(name)        G_STMT_START{          \
  2763.         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,              \
  2764.            "file %s: line %d (%s): unlocking: %s ",              \
  2765.            __FILE__,    __LINE__, G_GNUC_PRETTY_FUNCTION, \
  2766.                #name);                                            \
  2767.        g_static_mutex_unlock (&G_LOCK_NAME (name));               \
  2768.      }G_STMT_END
  2769. #    define G_TRYLOCK(name)        G_STMT_START{          \
  2770.         g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,              \
  2771.            "file %s: line %d (%s): try locking: %s ",         \
  2772.            __FILE__,    __LINE__, G_GNUC_PRETTY_FUNCTION, \
  2773.                #name);                                            \
  2774.      }G_STMT_END,    g_static_mutex_trylock (&G_LOCK_NAME (name))
  2775. #  else     /* !G_DEBUG_LOCKS */
  2776. #    define G_LOCK(name) g_static_mutex_lock       (&G_LOCK_NAME (name)) 
  2777. #    define G_UNLOCK(name) g_static_mutex_unlock   (&G_LOCK_NAME (name))
  2778. #    define G_TRYLOCK(name) g_static_mutex_trylock (&G_LOCK_NAME (name))
  2779. #  endif /* !G_DEBUG_LOCKS */
  2780. #else    /* !G_THREADS_ENABLED */
  2781. #  define G_LOCK_DEFINE_STATIC(name)    extern void glib_dummy_decl (void)
  2782. #  define G_LOCK_DEFINE(name)        extern void glib_dummy_decl (void)
  2783. #  define G_LOCK_EXTERN(name)        extern void glib_dummy_decl (void)
  2784. #  define G_LOCK(name)
  2785. #  define G_UNLOCK(name)
  2786. #  define G_TRYLOCK(name)        (FALSE)
  2787. #endif    /* !G_THREADS_ENABLED */
  2788.  
  2789. #ifdef __cplusplus
  2790. }
  2791. #endif /* __cplusplus */
  2792.  
  2793.  
  2794. #endif /* __G_LIB_H__ */
  2795.